I have an alarm manager that triggers at regular interval. A button in activity_main triggers it.
It works fine. Without any problem. Alarm manager is defined in Main class. A method (activated on button click) invokes the alarm manager. Another method should stop it or cancel it.
Code:
public class MainActivity extends Activity {
private PendingIntent pi;
private AlarmManager alarmManager;
.......................................
......................
public void activate(View view) {
//this activates the alarm manager.
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pi.getBroadcast(this, 1, i, pi.FLAG_UPDATE_CURRENT));
}
public void deactivate(View view) {
//this should Deactivate the alarm manager.
alarmManager.cancel(pi);
}
}
However, the second method is not working. (deactivate).
When the 2nd button is clicked, the App will crash!!
Log cat gives the reason as :
Caused by: java.lang.NullPointerException.
Any idea on how to fix this? Should I create an intent again in second method? I tried it but no use.. Any suggestions or fixes for this??