将Context传递给Class会导致getSystemService返回Null指针异常

时间:2018-01-25 18:32:18

标签: java android

这让我发疯了......过去2个小时我一直试图解决这个问题,但我无法弄清楚发生了什么。

我对Android开发相对较新,所以也许我错过了一个经验丰富的人可以帮助我的东西。

我只是想创建一个Notifier类,我可以调用它来向用户发送通知。

public class Notifier {

private Context mContext;

public Notifier(Context mContext){
    this.mContext = mContext;
}

NotificationCompat.Builder mBuilder =
    (NotificationCompat.Builder) new NotificationCompat.Builder(mContext)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("foo")
        .setContentText("bar")
        .setOngoing(true);

NotificationManager mNotifyMgr =
      (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);


public void showNotification(){
    mNotifyMgr.notify(001, mBuilder.build());
}
}

主:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    Notifier notifier = new Notifier(getApplicationContext());
}

错误:

01-25 13:11:25.211 27537-27537/net.alexdespain.mediamute E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: net.alexdespain.mediamute, PID: 27537
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{net.alexdespain.mediamute/net.alexdespain.mediamute.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                               at net.alexdespain.mediamute.Notifier.<init>(Notifier.java:27)
                                                                               at net.alexdespain.mediamute.MainActivity.onCreate(MainActivity.java:25)
                                                                               at android.app.Activity.performCreate(Activity.java:6679)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                               at java.lang.reflect.Method.invoke(Native Method)

错误似乎是由:

引起的
NotificationManager mNotifyMgr =
      (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);

但我不明白为什么mContext会为null ...

1 个答案:

答案 0 :(得分:3)

NotificationManager mNotifyMgr =
  (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);

这是一个带初始化程序的字段。初始化程序将在构造函数之前调用。此时mContextnull。初始化构造函数内的字段(和mBuilder字段):

public Notifier(Context mContext){
    this.mContext = mContext;
    mNotifyMgr =
      (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
    mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(mContext)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("foo")
    .setContentText("bar")
    .setOngoing(true);
}