当前位置: 代码迷 >> 综合 >> 解决:Android出现All WebView methods must be called on the same thread.
  详细解决方案

解决:Android出现All WebView methods must be called on the same thread.

热度:39   发布时间:2024-01-13 22:07:47.0
new Thread(){
            public void run() {
                //System.out.println("init-66666666662----");
                view.loadUrl(url);
            }

        }.start();

在新的线程中操作 WebView会报错

04-26 14:52:22.645: E/AndroidRuntime(7635): java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'Thread-666'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {35b6f8e5} called on null, FYI main Looper is Looper (main, tid 1) {35b6f8e5})

是因为 新版的Android的SDK要求在创建WebView所在的线程中操作它,在其它线程中操作它都会报这样的错误。

解决方法

WebViewActivity.this.runOnUiThread(new Runnable() {//其中 WebViewActivity 是webview所在的Activity    
            public void run() {
                System.out.println("init-66666666662----");
                webview.loadUrl(url);
            }
        });

  相关解决方案