Android BadTokenException when using WebView container

So, I followed the WebView memory leak exception message, which suggests using the webview container, and then programmatically add / remove the web view from the container in activity codes: Memory leak in WebView

However, I hit the following crash when I click on an html element that asks for a list of options to select (e.g. date / month drop-down menu)

W/dalvikvm(17767): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) W/WindowManager(129): Attempted to add window with non-application token WindowToken{4094b730 token=null}. Aborting. FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application android.view.ViewRoot.setView(ViewRoot.java:561) android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) android.app.Dialog.show(Dialog.java:265) android.webkit.WebView$InvokeListBox.run(WebView.java:9170) android.os.Handler.handleCallback(Handler.java:587) android.os.Handler.dispatchMessage(Handler.java:92) android.os.Looper.loop(Looper.java:150) android.app.ActivityThread.main(ActivityThread.java:4263) java.lang.reflect.Method.invokeNative(Native Method) java.lang.reflect.Method.invoke(Method.java:507) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) dalvik.system.NativeStart.main(Native Method) 

My layout has the following:

 FrameLayout android:id="@+id/webview_container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/titlebar"> /FrameLayout> 

I have the following in onCreate ():

 mWebViewContainer = (FrameLayout)findViewById(R.id.webview_container); mWebView = new WebView(getApplicationContext()); mWebViewContainer.addView(mWebView); mWebView.setWebChromeClient(new WebChromeClient()); 

I also install WebViewClient.

I checked that mWebView.getWindowToken () returns a non-zero value.

Any ideas as to why this issue could happen?

Edit: I experimented a bit more and looked around, but still haven't solved this problem. Everything works fine if I put the webview directly into the layout itself. But I do not want to do this, because I want to be able to dynamically change web views.

+6
android webview
source share
2 answers

When you create a WebView, you are currently using the application context. You must use an Activity context. To solve the problem, replace getApplicationContext() with this when creating the WebView.

+9
source share

The problem is here:

  mWebView.setWebChromeClient(new WebChromeClient()); 

after you leave the event, there is a chance that some callbacks in WebChromeClient will try to open dialogs while the activity of the container has been destroyed.
Here is one solution that works for me, just add mWebView.destroy () to your onDestroy () activity

  @Override public void onDestroy() { super.onDestroy(); if (mWebView != null) mWebView.destroy(); } 
+13
source share

All Articles