[android]NullPointer 异常在 Fragment 尝试实例化另一个类的方法时

发布时间: 2016/9/26 4:11:31
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想检查互联网连接 fragment 的实现 ConnectivityReceiver.ConnectivityReceiverListener。代码如下

ConnectivityReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectivityReceiver extends BroadcastReceiver {

public static ConnectivityReceiverListener connectivityReceiverListener;

public ConnectivityReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent arg1) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (connectivityReceiverListener != null) {
        connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
    }
}

public static boolean isConnected() {
    ConnectivityManager
            cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
}


public interface ConnectivityReceiverListener {
    void onNetworkConnectionChanged(boolean isConnected);
}
}

MyApplication.java

import android.app.Application;

public class MyApplication extends Application {

private static MyApplication mInstance;

@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;
}

public static synchronized MyApplication getInstance() {
    return mInstance;
}

public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
    ConnectivityReceiver.connectivityReceiverListener = listener;
}

}

Fragmentactivity具有代码

@Override
public void onResume() {
    super.onResume();

    // register connection status listener
  MyApplication.getInstance().setConnectivityListener(this);
}

我有"这个"值更改为 getactivity() 上, 过课。不,它没有产生任何结果。堆栈 trace 当我用这个如下

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: highski.developers.cflash, PID: 26183
                                                                       java.lang.RuntimeException: Unable to resume activity {highski.developers.cflash/highski.developers.cflash.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void highski.developers.cflash.connection_check.MyApplication.setConnectivityListener(highski.developers.cflash.connection_check.ConnectivityReceiver$ConnectivityReceiverListener)' on a null object reference
                                                                           at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
                                                                           at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void highski.developers.cflash.connection_check.MyApplication.setConnectivityListener(highski.developers.cflash.connection_check.ConnectivityReceiver$ConnectivityReceiverListener)' on a null object reference
                                                                           at highski.developers.cflash.fragments.HomeFragment.onResume(HomeFragment.java:362)
                                                                           at android.support.v4.app.Fragment.performResume(Fragment.java:2020)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
                                                                           at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2056)
                                                                           at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:196)
                                                                           at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:504)
                                                                           at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:493)
                                                                           at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178)
                                                                           at android.app.Activity.performResume(Activity.java:6089)
                                                                           at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
                                                                           at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:135) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

请让我知道什么是错误,如何解决这个问题。

解决方法 1:

第一次检查如果 getInstance 方法将返回 null,如果因此检查 Android Manifest


manifest 应包含的东西像这样︰

 <application
    android:name=".yourpackage.MyApplication"
    android:label="@string/app_name"

 more code here.
官方微信
官方QQ群
31647020