删除通知会导致崩溃

时间:2013-05-20 17:03:02

标签: android notifications

我在删除现有通知时遇到问题。我查看过其他帖子,看不出我做错了什么。这是设置它的代码

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);
    int id = (int) ((long) rowId);

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setContentText(notificationContent)
    .setContentTitle(notificationTitle)
    .setSmallIcon(smallIcon)
    .setAutoCancel(true)
    .setTicker(notificationTitle)
    .setLargeIcon(largeIcon)
    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
    .setContentIntent(pendingIntent);


    Notification notification = notificationBuilder.build();
    notificationManager.notify(id, notification);
}

以下是我尝试删除通知的地方:

public void cancelnote(long mRowId) {
    // TODO Auto-generated method stub
    int id = (int) mRowId;
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    Log.e(TAG2, "changed to id" + id);
    notificationManager.cancel(id);


}

这是它崩溃的地方。

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

这是错误日志

05-20 11:00:12.364: W/dalvikvm(3159): threadid=1: thread exiting with uncaught exception (group=0x414e6438)
05-20 11:00:12.384: E/AndroidRuntime(3159): FATAL EXCEPTION: main
05-20 11:00:12.384: E/AndroidRuntime(3159): java.lang.NullPointerException
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.opd247.OPD247.ReminderCancel.cancelnote(ReminderCancel.java:28)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.opd247.OPD247.meds.onContextItemSelected(meds.java:191)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.app.Activity.onMenuItemSelected(Activity.java:2592)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.opd247.OPD247.meds.onMenuItemSelected(meds.java:157)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3859)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:193)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:934)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.widget.AbsListView.performItemClick(AbsListView.java:1268)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3059)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.widget.AbsListView$1.run(AbsListView.java:3950)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.os.Handler.handleCallback(Handler.java:615)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.os.Looper.loop(Looper.java:137)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at android.app.ActivityThread.main(ActivityThread.java:4918)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at java.lang.reflect.Method.invokeNative(Native Method)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at java.lang.reflect.Method.invoke(Method.java:511)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-20 11:00:12.384: E/AndroidRuntime(3159):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

我建议从该行中删除getApplicationContext()。这对我来说很好 -

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

答案 1 :(得分:0)

你可以试试这个:

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

这对我有用......