Android 清除状态通知

Android 清除状态通知,android,android-notification-bar,Android,Android Notification Bar,我很难清除状态栏通知 public void NotificationStatus(){ Intent intent = new Intent(this, Stimulation.class); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notificatio

我很难清除状态栏通知

public void NotificationStatus(){

    Intent intent = new Intent(this, Stimulation.class);   
    NotificationManager notificationManager
        = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(
        david.flanagan.android.VenAssist.Activities.R.drawable.notification_icon,
        "VenAssist Stimulation ON", System.currentTimeMillis());
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.flags |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.setLatestEventInfo(this, "VenAssist",
        "Stimulation Started", activity);
    notificationManager.notify(0, notification);     
}
然后,我试图通过调用

notification.cancel(0);
我已经创建了一个通知实例,我不确定它是否正确

private NotificationManager notification;

当我尝试取消服务,因此清除状态栏时,应用程序崩溃。

您的通知对象可能为null,这将导致
NullPointerException

我认为您应该使用
NotificationManager
对象来取消通知,使用相同的id(在您的情况下为0)并将其传递给
NotificationManager。取消(Int)


我建议您粘贴stacktrace
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);