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

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

问题描述

于是,我跟着一个职位上避免的WebView内存泄漏,这表明使用的WebView容器中,然后以编程方式添加/从容器中活动codeS删除的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?

编辑: 我已经做了一些尝试,并四处寻找,但还是没有解决这个问题。一切功能正常,如果我在布局本身把web视图直接。但我不想这样做,因为我希望能够动态地交换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。 你应该使用活动的背景下。为了解决这个问题,替换 getApplicationContext()当您创建web视图。

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天全站免登陆