错误的异常"在线程' WebViewCoreThread'上调用WebView方法。"

时间:2014-04-04 12:49:38

标签: android webview

我在三星Tab 8.9上尝试一个简单的StackTrace项目时记录了WebView

04-04 13:32:26.688: D/core(9992): core.cpp  228 bind    alias=User
04-04 13:32:26.708: W/webview(9992): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
04-04 13:32:26.708: W/webview(9992):    at android.webkit.WebView.checkThread(WebView.java:14244)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.WebView.getSettings(WebView.java:5378)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.WebView.getVisibleTitleHeightImpl(WebView.java:2343)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.WebView.getViewHeight(WebView.java:2385)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.WebViewCore.setupViewport(WebViewCore.java:3422)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.WebViewCore.didFirstLayout(WebViewCore.java:3149)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:113)
04-04 13:32:26.708: W/webview(9992):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 13:32:26.708: W/webview(9992):    at android.os.Looper.loop(Looper.java:137)
04-04 13:32:26.708: W/webview(9992):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:828)
04-04 13:32:26.708: W/webview(9992):    at java.lang.Thread.run(Thread.java:856)
04-04 13:32:26.718: W/webview(9992): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
04-04 13:32:26.718: W/webview(9992):    at android.webkit.WebView.checkThread(WebView.java:14244)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.WebView.getSettings(WebView.java:5378)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.WebView.getVisibleTitleHeightImpl(WebView.java:2343)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.WebView.getViewHeight(WebView.java:2385)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.WebViewCore.setupViewport(WebViewCore.java:3422)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.WebViewCore.didFirstLayout(WebViewCore.java:3149)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:113)
04-04 13:32:26.718: W/webview(9992):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 13:32:26.718: W/webview(9992):    at android.os.Looper.loop(Looper.java:137)
04-04 13:32:26.718: W/webview(9992):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:828)
04-04 13:32:26.718: W/webview(9992):    at java.lang.Thread.run(Thread.java:856)

据我了解,WebView说我在WebView线程上调用WebView方法 - 而不是在UI线程上调用。我的问题是,除了WebView方法之外,我严格触及onCreate() - 这种方法始终作为Activity lifecycle的一部分运行AFAIK总是在UI线程上......

@Override
protected void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);

    WebView mWebView = new WebView(this);
    mWebView.loadUrl("file:///android_asset/index.htm");
    mWebView.getSettings().setJavaScriptEnabled(true);      
    mWebView.addJavascriptInterface(new CoreModInterface(), "Android");
    mWebView.setWebViewClient(new WebViewClient());

    setContentView(mWebView);
}

此警告在Nexus设备上不会发生。这是Samsung Tab 8.9的问题吗?

修改

删除这两行会删除错误。在显式runOnUiThread()内的相同位置添加这两行也会产生相同的错误。在这个阶段,我再次相信三星再犯了一个错误。

    mWebView.getSettings().setJavaScriptEnabled(true);      
    mWebView.addJavascriptInterface(new CoreModInterface(), "Android");

0 个答案:

没有答案