应用程序崩溃onResume当取消通知 [英] App crashes onResume when canceling Notification

查看:257
本文介绍了应用程序崩溃onResume当取消通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to cancel my Notification in onResume() but it crashes:

使用displayNotification()我创建通知。我也试着设置cancelNotification()在尝试捕捉。但是,这并不解决问题。但是,如果没有我什至不能启动应用程序生成的通知。

With displayNotification() i create the Notification. I also tried to set the cancelNotification() in a try catch. But this doesn't solve the problem. But without i can't even start the app to generate the Notification.

下面是我的codesnippets:

Here are my Codesnippets:

OnResume:

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

cancelNotification():

cancelNotification():

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

displayNotification():

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

有什么建议?

推荐答案

mNotificationManager cancelNotification 空。只是它重新实例:

mNotificationManager is null in cancelNotification. Just reinstantiate it:

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

这篇关于应用程序崩溃onResume当取消通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆