I have a two timers:
- theFirstCountDownTimer;
- theGraceCountDownTimer;
I have a service that uses these two timers where the theGraceCountDownTimer is nested within theFirstCountdownTimer. On the OnDestroy Method, I want to cancel both timers.
private CountDownTimer theFirstCountDownTimer;
private CountDownTimer theGraceCountDownTimer;
//////////////////////////////////////
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
//////////////////////////////////////
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.i("Running", "The Service");
final SharedPreferences myPref = this.getSharedPreferences("testPref321", Context.MODE_PRIVATE);
theFirstCountDownTimer = new CountDownTimer(myPref.getInt("seekBar", 10000) * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
String myMilis = Long.toString(millisUntilFinished);
Log.i("Timer One", "OnTick-" + myMilis);
}
@Override
public void onFinish() {
Log.i("Timer One", "OnFinish");
theGraceCountDownTimer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
String myMilis = Long.toString(millisUntilFinished);
Log.i("Second Timer!", myMilis);
}
@Override
public void onFinish() {
Log.i("Finished!", "Heck Ya!");
}
}.start();
}
}.start();
return START_STICKY;
}
//////////////////////////////////////
@Override
public void onDestroy(){
theGraceCountDownTimer.cancel();
//This is the error
theFirstCountDownTimer.cancel();
super.onDestroy();
}
Additionally, I know the error is specifically with the line, theGraceCountDownTimer.cancel.
The error states:
Process: com.example.jackson.dmtapp, PID: 22854
java.lang.RuntimeException: Unable to stop service com.example.jackson.dmtapp.TheService@8090f39: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
at android.app.ActivityThread.handleStopService(ActivityThread.java:3479)
at android.app.ActivityThread.-wrap27(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1638)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
at com.example.jackson.dmtapp.TheService.onDestroy(TheService.java:93)
at android.app.ActivityThread.handleStopService(ActivityThread.java:3462)
at android.app.ActivityThread.-wrap27(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1638)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)