我已使用PendingIntent
设置闹钟,现在我想取消闹钟。但是,尝试这样做会导致整个应用程序崩溃。
以下是我设置闹钟的方法:
private void setAlarm(Calendar targetCal)
{
Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class);
alarmintent.putExtra("ALARM_NO", tempx);
alarmintent.setAction("my.action.string");
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);
}
其中pen是UniqueId
现在,我取消闹钟时出现错误。
public void alarmSet1(View view)
{
//ToggleButton Toggler = (ToggleButton)findViewById(R.id.alarm1);
int a1=1;
int idTime = (int) System.currentTimeMillis();
SharedPreferences.Editor editor = times.edit();
editor.putInt("ID1", idTime);
editor.commit();
if (((ToggleButton) view).isChecked()) {
Intent intent = new Intent(MainActivity.this, AddAlarm.class);
intent.putExtra("pendInt", idTime);
intent.putExtra("tts", a1);
startActivity(intent);
}
else
{
int rec = times.getInt("ID1",0);
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,
rec, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
}
}
以下是logcat:
> 07-29 20:19:06.198 14220-14220/zyia.alarm.zyia.zyia E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: zyia.alarm.zyia.zyia, PID: 14220
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlarmManager.cancel(android.app.PendingIntent)' on a null object reference
at zyia.alarm.zyia.zyia.MainActivity.alarmSet1(MainActivity.java:150)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
寻找任何修复/解决方案:)
答案 0 :(得分:3)
此行抛出NullPointerException:
alarmManager.cancel(pendingIntent);
可能是因为alarmManager未初始化。在第一行上方添加此行。
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
答案 1 :(得分:1)
alarmSet1()
未初始化alarmManager
,它是null
,如堆栈跟踪所示。我不知道你在哪里初始化alarmManager
。最有可能的是,你应该摆脱alarmManager
并让setAlarm()
和alarmSet1()
根据需要检索AlarmManager
。
答案 2 :(得分:1)
看起来alarmManager
未被初始化。