2

I have made and simple countdown timer app, but when i try to make onDestroy to cancel countdown timer i get and error. My onDestroy codeblock:

    @Override
    public void onDestroy()
    {
    super.onDestroy();
    countdowntimer.cancel();
    }

And LogCat error

12-18 19:16:06.383: E/AndroidRuntime(25512): FATAL EXCEPTION: main
12-18 19:16:06.383: E/AndroidRuntime(25512): java.lang.RuntimeException: Unable to destroy        activity {com.android.SquirellMusic/com.android.SquirellMusic.SquirellMusicActivity}: java.lang.NullPointerException
12-18 19:16:06.383: E/AndroidRuntime(25512):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3106)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3171)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at android.app.ActivityThread.access$2100(ActivityThread.java:132)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1071)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at      android.os.Looper.loop(Looper.java:150)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at android.app.ActivityThread.main(ActivityThread.java:4293)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at java.lang.reflect.Method.invokeNative(Native Method)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at java.lang.reflect.Method.invoke(Method.java:507)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at dalvik.system.NativeStart.main(Native Method)
12-18 19:16:06.383: E/AndroidRuntime(25512): Caused by: java.lang.NullPointerException
12-18 19:16:06.383: E/AndroidRuntime(25512):    at com.android.SquirellMusic.SquirellMusicActivity.onDestroy(SquirellMusicActivity.java:364)
12-18 19:16:06.383: E/AndroidRuntime(25512):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3088)

Any help is appreciated

v66dik
  • 247
  • 3
  • 11

2 Answers2

1

The answer lies in the following concept of onDestroy() of the activity :

  • onDestroy() is called automatically by the DVM. After this, activity will be removed from the stack.

Now, your code for countdowntimer.cancel is written after the super(), so the activity (alongwith your countdowntimer object) is getting removed from the memory before your countdowntimer.cancel.

So just moving your super.onDestroy() at the end of the onDestroy() method will avoid app crashing.

Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84
  • i tried it and still got the same error. Is there any other possibility to cancel countdown timer when app quits? – v66dik Dec 18 '11 at 19:34
0

The best solution will be to save the current time and stop the timer in onPause, then restart the timer in onResume, adding to it the saved time. [for paused state] To make sure that at the launch of app the timer starts from the begining, just assign the saved time varible 0 on onStop() [for start case]

Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84