I am New in Android and I create a application but it crash when I start same activity on button click and i also finish current Activity here is my error log . I am using Timer in my application and Background music also
12-18 05:55:15.776: D/AndroidRuntime(2345): Shutting down VM
12-18 05:55:15.776: W/dalvikvm(2345): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-18 05:55:15.817: E/AndroidRuntime(2345): FATAL EXCEPTION: main
12-18 05:55:15.817: E/AndroidRuntime(2345): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41542480 is not valid; is your activity running?
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.view.Window$LocalWindowManager.addView(Window.java:547)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.app.Dialog.show(Dialog.java:277)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
12-18 05:55:15.817: E/AndroidRuntime(2345): at com.example.whowantto.play$timer.onFinish(play.java:722)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.os.Looper.loop(Looper.java:137)
12-18 05:55:15.817: E/AndroidRuntime(2345): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 05:55:15.817: E/AndroidRuntime(2345): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 05:55:15.817: E/AndroidRuntime(2345): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 05:55:15.817: E/AndroidRuntime(2345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 05:55:15.817: E/AndroidRuntime(2345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 05:55:15.817: E/AndroidRuntime(2345): at dalvik.system.NativeStart.main(Native Method)
12-18 05:55:57.476: E/Trace(2564): error opening trace file: No such file or directory (2)
12-18 05:55:57.546: D/AndroidRuntime(2564): Shutting down VM
Thanks in advance
Your
CountDownTimertries to show anAlertDialogafter theActivityhas ended it’s lifecycle. Maybe you are callingfinish()but don’tcancel()the task.You could add a variable that is checked by your timer if it should still show the dialog like in below sample
Or you could go with a more sophisticated implementation that should retain the timer task and re-attach it to the next actvity instance like in below example. This way the Activity can be recreated on rotation for example and still use a timer started in an old activity instance.
All code untested but it’s roughly how it should work.