9

I'm working on an Android application that has multiple activities and services. Some of the activities are defined in 3rd party libraries that I'm importing into my project and the problem is that on some devices (specially in Samsung Galaxy Tabs) my app keeps crashing when switching from one activity to the previous via clicks on the back button.

I did a LogCat and found this to be the cause of the crashes:

android.view.WindowManager$BadTokenException: Unable to add window -- token
  android.os.BinderProxy@351c808e is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

but the interesting thing is that none of my application's classes appears on the stack trace. I searched for similar problems on StackOverflow but every other report I found that had a stack trace of a BadTokenException, the respective application's code always appeared in the stack trace.

Does anyone know what are the common causes for this problem and/or the best way to troubleshoot it? I noticed that this problem happens more on Samsung devices so maybe it's an actual bug on those devices.

  • were you able to find a cause / fix for the problem ? – kiruwka Feb 04 '15 at 16:11
  • 1
    Yes. The problem was that my code had an event listener that held a reference to a TextView. This TextView's context was an activity that had already been destroyed and the `BadTokenException` was being thrown when the listener tried to call `setText()`. Most of the times this resulted in a silent failure but on some devices it would cause the UI thread to throw an exception and crash the app. –  Feb 05 '15 at 02:48
  • interesting. How did you manage to pinpoint this to `setText` ? Did you use extra logs (perhaps stacktraces of other threads ? Do you think you could post an answer with some code/directions how to debug this, would be highly appreciated. Thanks! – kiruwka Feb 05 '15 at 07:17
  • I tried to reproduce scenario you mentioned, having a button reference obsolete context of destroyed activity. When I call `setText`, is simply has no effect in this case, but no crash : ( – kiruwka Feb 05 '15 at 08:08
  • This problem might be specific to Samsung devices. My Nexus 4 and Nexus 7 (running KitKat at the time) didn't have this problem but my Samsung Galaxy Tab 10 and Samsung Note 2 (also running KitKat) would fail with this message. –  Feb 06 '15 at 21:25
  • Thanks a lot, really appreciate it. So far no luck in reproducing, but I will try again, following your leads. Do you happen to have any sample code for your scenario ? Anyways, thank.s – kiruwka Feb 10 '15 at 10:25
  • I am also getting same logcat and i am not able to produce. can anyone please let me know that may be the reasons. – Sunit Kumar Gupta May 06 '16 at 07:08
  • See my answer in this topic: http://stackoverflow.com/a/39091647/6745903 It works well for me. – Matrixxun Aug 23 '16 at 03:34

1 Answers1

0

Usually BadTokenExceptions occur when your activity attempts to create a new Window before its onAttachToWindow() method is called (or after its onDetachFromWindow() method is called). It could very well be that the third party libraries you are using are buggy and do not ensure that this requirement is met.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 1
    I am having very same strack trace (in production, not reproducible locally). Could you please recommend some approach to locate code / scenario that could cause this ? I tried suggestion from OP's comment with no luck to reproduce it. I would really like to fix it. Many thanks. – kiruwka Feb 05 '15 at 08:12