我有一个Android应用程序,我使用待定意图设置通知。我没有设置闹钟的问题。但是,如果退出应用程序,则会发生空指针异常。有帮助吗?感谢
警报服务
int id = intent.getIntExtra("sidAlarm",0);
Log.w("id",Integer.toString(id));
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainMenu.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),id, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(id, notification);
创建待定意图
//Create intent for delete
Intent myIntent = new Intent(SessionDetails.this, MyReceiver.class);
myIntent.putExtra("sidAlarm", pendingIntentId);
pendingIntentToDelete = PendingIntent.getBroadcast(SessionDetails.this, pendingIntentId, myIntent,0);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntentToDelete);
Logcat跟踪
01-15 18:41:40.584: E/AndroidRuntime(8072): FATAL EXCEPTION: main
01-15 18:41:40.584: E/AndroidRuntime(8072): java.lang.RuntimeException: Unable to start service com.example.MyAlarmService@4291ff68 with null: java.lang.NullPointerException
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850)
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.app.ActivityThread.access$2000(ActivityThread.java:159)
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419)
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.os.Looper.loop(Looper.java:137)
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-15 18:41:40.584: E/AndroidRuntime(8072): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 18:41:40.584: E/AndroidRuntime(8072): at java.lang.reflect.Method.invoke(Method.java:525)
01-15 18:41:40.584: E/AndroidRuntime(8072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
01-15 18:41:40.584: E/AndroidRuntime(8072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
01-15 18:41:40.584: E/AndroidRuntime(8072): at dalvik.system.NativeStart.main(Native Method)
01-15 18:41:40.584: E/AndroidRuntime(8072): Caused by: java.lang.NullPointerException
01-15 18:41:40.584: E/AndroidRuntime(8072): at com.example.MyAlarmService.onStart(MyAlarmService.java:39)
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.app.Service.onStartCommand(Service.java:450)
01-15 18:41:40.584: E/AndroidRuntime(8072): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2833)
01-15 18:41:40.584: E/AndroidRuntime(8072): ... 10 more