我在后台运行通知服务。当有新任务时会弹出。当应用程序关闭并弹出通知时,如果我点击它,它将转到MyHomePage.class。但是,当App正在运行时,我处于不同的活动状态并弹出。它抛出一个错误,说NullPointerException。我究竟做错了什么。为什么我可以从当前活动跳转到MyHomePage.class?
private void createNewTaskNotification(String s, String id) {
CharSequence title = "TASK";
CharSequence message = s;
notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.checkyes, s,
System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
Intent notificationIntent = new Intent(context, MyHomePage.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP );
PendingIntent pendingIntent = PendingIntent.getActivity(context,
Integer.parseInt(id), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, pendingIntent);
notificationManager.notify(Integer.parseInt(id), notification);
}
logcat的:
11-07 12:05:38.870: ERROR/AndroidRuntime(788): FATAL EXCEPTION: main
11-07 12:05:38.870: ERROR/AndroidRuntime(788): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aa.me.vianet/com.aa.me.vianet.activity.MyHomePage}: java.lang.NullPointerException
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.os.Looper.loop(Looper.java:132)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.app.ActivityThread.main(ActivityThread.java:4028)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at java.lang.reflect.Method.invoke(Method.java:491)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): Caused by: java.lang.NullPointerException
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at com.aa.me.vianet.activity.MyHomePage.onCreate(MyHomePage.java:46)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-07 12:05:38.870: ERROR/AndroidRuntime(788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)