Reputation: 1051
I am getting below crash exclusively at app launch time and only in Emulator & Debug
Emulator & Run : OK
Device & Run: OK
Device & Debug: OK
Emulator & Debug: CRASH!
It looks like Android is trying to do a Toast at launch time, and as I don't have many Toast and having added a breakpoint on all of them, I can exclude that it is one of mine.
Is there a way to find out what message Android is trying to show and to which Activity it is referring? Logcat doesn't show anything abnormal before the crash occurs.
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@e4f4f2b is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.widget.Toast$TN.handleShow(Toast.java:459)
at android.widget.Toast$TN$2.handleMessage(Toast.java:342)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Upvotes: 6
Views: 5290
Reputation: 1602
This is most likely happening because you are trying to show a dialog after the execution of a background thread, while the Activity is being destroyed.
Here's what worked for me:
if(!((Activity) context).isFinishing())
{
//show dialog
}
It's working perfectly for me, using this practice for years now.
Upvotes: 3