取消通知时,应用程序崩溃onResume

时间:2014-10-23 15:33:17

标签: android android-notifications android-notification-bar

我尝试在onResume()中取消我的通知但它崩溃了:

使用displayNotification()我创建通知。我还尝试在try catch中设置cancelNotification()。但这并没有解决问题。但是,如果没有我甚至无法启动应用程序来生成通知。

以下是我的Codesnippets:

的onResume:

@Override
    protected void onResume() //activity was resumed and is visible again 
    {
        Log.d(logtag,"onResume() called");
        super.onResume();
        cancelNotification();
    }

cancelNotification():

protected void cancelNotification() 
    {
        Log.i("Cancel", "notification");
        mNotificationManager.cancel(1);
    }

displayNotification():

protected void displayNotification(String message, String ticker) 
    {
        Log.i("Start", "notification");

        /* Invoking the default notification service */
        NotificationCompat.Builder  mBuilder = 
        new NotificationCompat.Builder(this);   

        mBuilder.setContentTitle("Neue Nachricht!");
        mBuilder.setContentText(message);
        mBuilder.setTicker(ticker);
        mBuilder.setSmallIcon(R.drawable.ic_launcher);

        /* Increase notification number every time a new notification arrives */
        mBuilder.setNumber(++numMessages);

        /* Creates an explicit intent for an Activity in your app */
        Intent resultIntent = new Intent(this, MainActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);

        /* Adds the Intent that starts the Activity to the top of the stack */
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

        mBuilder.setContentIntent(resultPendingIntent);

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        /* notificationID allows you to update the notification later on. */
        mNotificationManager.notify(1, mBuilder.build());
    }

错误:

10-23 17:27:35.763: E/AndroidRuntime(349): FATAL EXCEPTION: main
10-23 17:27:35.763: E/AndroidRuntime(349): java.lang.RuntimeException: Unable to resume activity {com.example.blauzahn/com.example.blauzahn.MainActivity}: java.lang.NullPointerException
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.os.Looper.loop(Looper.java:123)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-23 17:27:35.763: E/AndroidRuntime(349):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 17:27:35.763: E/AndroidRuntime(349):  at java.lang.reflect.Method.invoke(Method.java:507)
10-23 17:27:35.763: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-23 17:27:35.763: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-23 17:27:35.763: E/AndroidRuntime(349):  at dalvik.system.NativeStart.main(Native Method)
10-23 17:27:35.763: E/AndroidRuntime(349): Caused by: java.lang.NullPointerException
10-23 17:27:35.763: E/AndroidRuntime(349):  at com.example.blauzahn.MainActivity.cancelNotification(MainActivity.java:388)
10-23 17:27:35.763: E/AndroidRuntime(349):  at com.example.blauzahn.MainActivity.onResume(MainActivity.java:976)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.Activity.performResume(Activity.java:3832)
10-23 17:27:35.763: E/AndroidRuntime(349):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
10-23 17:27:35.763: E/AndroidRuntime(349):  ... 12 more

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

{p} mNotificationManagercancelNotification中为空。只需重新实例化它:

protected void cancelNotification() {
    Log.i("Cancel", "notification");
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(1);
}

答案 1 :(得分:1)

@ EgnorN的回答是正确的:mNotificationManagercancelNotification()为空。

添加更广泛的课程:

  1. 我们可以从堆栈跟踪中调试这样的问题:
  2.     Caused by: java.lang.NullPointerException
          at ...cancelNotification(MainActivity.java:388)
          at ...onResume(MainActivity.java:976)
    

    这表示该程序在388行的MainActivity.java中的cancelNotification()中与空值相冲突。必须是:

        mNotificationManager.cancel(1);
    

    mNotificationManager必须为null才能导致这种情况。

    如果给定行上有多个可能为null的内容,可以使用调试器断点停在该行并查看不同的值。或者,将其分成多行并再次运行以查看抛出异常的行。

    1. 如果更改代码以在需要它的每个方法中获取NotificationManager并将其保存在局部变量中而不是将其保存在实例变量中,那么将避免出现类似问题的类别,其中代码错误地假定了Activity的实例变量仍然设置。
    2. 无论如何,请阅读Activity life cycle。了解何时创建和删除Activity实例非常重要。

      1. 如果您使用support.v4.app库的API 21版本构建项目,则可以使用新添加的方法{{1}使代码更简单,更安全,更向后兼容获取NotificationManagerCompat,它类似于NotificationManager以及旧平台的后备。