使用 WebView 容器时的 Android BadTokenException [英] Android BadTokenException when using a WebView Container

查看:19
本文介绍了使用 WebView 容器时的 Android BadTokenException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我关注了一篇关于避免 WebView 内存泄漏的帖子,它建议使用 webview 容器,然后在活动代码中以编程方式从容器中添加/删除 webview:WebView 内存泄漏

So I followed a post on avoiding WebView memory leaks, which suggests to use a webview container and then programmatically add/remove the webview from the container in activity codes: Memory leak in WebView

但是,我在单击 html 元素时遇到了以下崩溃,该元素提示要选择的选项列表(例如日期/月份下拉菜单)

However, I hit the following crash when clicking on an html element which prompts 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)

我的布局中有以下内容:

I have the following in my layout:

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

我在 onCreate() 中有以下内容:

I have the following in onCreate():

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

mWebView.setWebChromeClient(new WebChromeClient());

我还设置了一个 WebViewClient.

I also set a WebViewClient.

我已经验证 mWebView.getWindowToken() 确实返回了一个非空值.

I have verified that mWebView.getWindowToken() does return a non-null value.

有关为什么会发生此问题的任何想法?

Any ideas as to why this issue might be happening?

我做了更多的实验和环顾四周,但仍然没有解决这个问题.如果我将 webview 直接放在布局本身中,则一切正常.但我不想这样做,因为我希望能够动态交换 webviews.

I've done some more experimenting and looking around, but still haven't solved this issue. Everything functions fine if I put the webview directly in the layout itself. But I don't want to do that because I want to be able to dynamically swap webviews.

推荐答案

当您创建 WebView 时,您当前正在使用应用程序的上下文.您应该使用活动的上下文.为了解决这个问题,在创建 WebView 时将 getApplicationContext() 替换为 this.

When you create the WebView you are currently using the context of the application. You should be using the context of the Activity. To solve the problem, replace getApplicationContext() with this when you create the WebView.

这篇关于使用 WebView 容器时的 Android BadTokenException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆