如何从静态方法调用非静态方法

ObA*_*bAt 3 java static android non-static android-4.0-ice-cream-sandwich

可能重复:
在Java中调用静态方法中的非静态方法

是否可以从静态方法调用非静态方法?非静态位于另一个类中,它必须是非静态的.

    public static void start() {
        CheckConnection checkInternet = new CheckConnection();
        if (checkInternet.isNetworkAvailable()) {
            // Has internet Connection
            } else {
            // No Internet Connection
        }
 }
Run Code Online (Sandbox Code Playgroud)

代码在Eclipse(Android 4.0.4)中没有给出任何错误,但如果我运行它,我的应用程序会冻结并关闭.

Sum*_*ngh 5

The only way to call a non-static method from a static method is you should have
an instance of the class containing the non-static method.
Run Code Online (Sandbox Code Playgroud)

就像你的问题:

 CheckConnection checkInternet = new CheckConnection();
    if (checkInternet.isNetworkAvailable()) {
        // Has internet Connection
        } else {
        // No Internet Connection
Run Code Online (Sandbox Code Playgroud)

你有实例,CheckConnection所以你可以调用它.

所以你的代码中没有问题,静态方法的非静态方法可能是其他一些东西负责应用程序冻结.