Reputation: 6114
I've set an Alarm using PendingIntent
and now I want to cancel the alarm. However trying to do so, crashes the entire application.
Here is how I set the Alarm:
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);
}
where pen is the UniqueId
Now here is where I get error when I cancel the Alarm.
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);
}
}
Here is the logcat as well :
> 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)
Looking for any fix/solutions :)
Upvotes: 2
Views: 3624
Reputation: 6717
This line is throwing a NullPointerException:
alarmManager.cancel(pendingIntent);
Probably because alarmManager is not initialized. Add this line above the first.
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Upvotes: 4
Reputation: 1006674
alarmSet1()
does not initialize alarmManager
, and it is null
, as indicated by the stack trace. I do not know where you are initializing alarmManager
. Most likely, you should get rid of alarmManager
and have both setAlarm()
and alarmSet1()
retrieve an AlarmManager
as needed.
Upvotes: 1