5

I'm seeing this error in our crash logging coming from only Android 6.0.1 devices, but not device specific.

Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
       at android.view.ViewRootImpl.setView(ViewRootImpl.java:849)
       at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337)
       at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
       at android.widget.PopupWindow.invokePopup(PopupWindow.java:1329)
       at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1077)
       at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1035)
       at com.android.internal.widget.FloatingToolbar$FloatingToolbarPopup.show(FloatingToolbar.java:563)
       at com.android.internal.widget.FloatingToolbar.show(FloatingToolbar.java:214)
       at com.android.internal.view.FloatingActionMode$FloatingToolbarVisibilityHelper.updateToolbarVisibility(FloatingActionMode.java:411)
       at com.android.internal.view.FloatingActionMode$1.run(FloatingActionMode.java:65)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:158)
       at android.app.ActivityThread.main(ActivityThread.java:7229)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Does anyone know the cause/solution for this issue? It seems to occur relatively infrequently.

RyanCheu
  • 3,522
  • 5
  • 38
  • 47
  • 1
    Have the same issue. Happens 100% on Samsung 6.0.1 devices. I cannot reproduce it on my own Samsung device with 6.0.1. Driving me crazy. Did you find any solution? Also subscribe to this: https://code.google.com/p/android/issues/detail?id=208906 – peshkira Jul 13 '16 at 08:30
  • Nope, still haven't figured it out. – RyanCheu Jul 13 '16 at 17:41

2 Answers2

3

Have the same problem only on Samsung devices with Android 6.0.1 and above. Being able to reproduce it.

The problem is with the new floating action mode (floating toolbar)

It's happening when a user selects some text on the screen (floating toolbar appeared), goes to the previous activity (closes the current activity) and then, as far as I understood, android OS is trying to recreate it (because it wasn't properly closed) using the object of activity that has been destroyed.

The solution is to deal with new ActionMode properly and to close it before closing the activity.

nicolausYes
  • 633
  • 8
  • 33
  • Awesome! I couldn't recreate this bug and its driving me crazy... Thanks! Now, could you tell something more about how to deal with this when it shows on Activity with WebView? I don't create it programmatically, it shows automatically. – miecio Aug 19 '16 at 10:54
  • 4
    @miecio override startActionMode(Callback callback, int type) in your webview, and save a weak reference to the result of super.startActionMode(...) so that you can finish it in onPause if needed. – RyanCheu Aug 31 '16 at 01:06
0

Calling webview.onPause from the the activities onPause and webview.onResume() from the activities onResume also seems to fix this issue.

mjstam
  • 1,049
  • 1
  • 6
  • 5