Help with cancelling alarms [duplicate]
When user sets alarm and when i want to cancel alarm using a button i have a problem. If the user sets the alarm and then without leaving the app presses the butt开发者_开发百科on to cancel it it cancels fine. but if the user leaves the app and comes back and clicks the button cancel the alarm it wont work and it fires an error message.
SilentManager.mAlarmManager.cancel(SilentManager.pi); is the thing that keeps crashing if you leave the app and come back.
Use adb logcat
, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your "crash". Most likely, you will find that it is a NullPointerException
, because your process has been terminated and therefore your static pi
data member is null
.
If this is your error, the solution is:
Step #1: Get rid of the pi
static data member.
Step #2: When you cancel()
, create a PendingIntent
on an equivalent Intent
to the one you used to create the alarm in the first place.
精彩评论