Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android通知空指针异常_Android_Exception_Pointers_Null_Notifications - Fatal编程技术网

Android通知空指针异常

Android通知空指针异常,android,exception,pointers,null,notifications,Android,Exception,Pointers,Null,Notifications,我在活动中设置了通知。它按预期创建通知。当我回到主屏幕时,通知仍然在上面,很好。如果我点击通知,它会带我回到创建它的活动,太好了。如果我点击一个按钮取消通知,我会得到一个NullPointerException 这是电话: if (notificationManager == null) notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent =

我在活动中设置了通知。它按预期创建通知。当我回到主屏幕时,通知仍然在上面,很好。如果我点击通知,它会带我回到创建它的活动,太好了。如果我点击一个按钮取消通知,我会得到一个NullPointerException

这是电话:

if (notificationManager == null)
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Intent intent = new Intent(this, myClass.class);
Notification notification = new Notification(android.R.drawable.icon, getString(R.string.notify), System.currentTimeMillis());
notification.setLatestEventInfo(myClass.this, "AppName","Description", PendingIntent.getActivity(this.getBaseContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));

notificationManager.notify(1, notification);
这是取消通知:

private OnClickListener onClick_Buttons = new OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
            case R.id.btn:
                notificationManager.cancel(1);

                break;
            }
        } catch (Exception e) {
            Log.e(getString(R.string.app_name), e.getMessage());
        }
    }
};
下面是日志:

FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message
    at android.util.Log.println_native(Native Method)
    at android.util.Log.e(Log.java:215)
    at com.myApp.myClass$2.onClick(myClass.java:281)
    at android.view.View.performClick(View.java:2408)
    at android.view.View$PerformClick.run(View.java:8817)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
我错过了什么


谢谢一个小金属工人告诉我: 把这个`

if (notificationManager == null)
  notificationManager = (NotificationManager)  getSystemService(NOTIFICATION_SERVICE);

在取消行之前

logcat中的堆栈跟踪是什么?我已将其添加到问题中。ZygoteInit.java第868行是什么?我不知道。com.android.internal.os。。。。我的活动只有364行。事实上,我算出来了,但我会给你评分:)。回答:将这个“if(notificationManager==null)notificationManager=(notificationManager)getSystemService(NOTIFICATION_SERVICE);”在取消行之前。很高兴能被识别出来!:)希望应用程序的其余部分进展顺利。