Android 同一线程错误上的 WebView 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22607657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:14:35  来源:igfitidea点击:

WebView methods on same thread error

javascriptandroidandroid-webview

提问by Johan Hoeksma

I have a android program (Java + html in a webview). I can call from the javascript to the Java code. But the other way around stopped working (after updating in eclipse).

我有一个 android 程序(webview 中的 Java + html)。我可以从 javascript 调用 Java 代码。但反过来停止工作(在 eclipse 中更新后)。

So this is what I'm trying to do

所以这就是我想要做的

  • Make a webview (worked)
  • calling in javascript to AndroidFunction.test(); (worked)
  • the java test() function call webView.loadUrl("javascript:helloBack()"); (! not working anymore)
  • 制作一个网页视图(工作)
  • 在 javascript 中调用 AndroidFunction.test(); (工作)
  • java test() 函数调用 webView.loadUrl("javascript:helloBack()"); (!不再工作了)

I tried to let it work with the WebView in the MainActivity, but it didnt work.

我试图让它与 MainActivity 中的 WebView 一起工作,但它没有工作。

MainActivity.java

主活动.java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        javascr = new Javascript(this, webView);
        webView.addJavascriptInterface(javascr, "AndroidFunction");
        webView.loadUrl("file:///android_asset/www/index.html");

        ....
}

Javascript.java

Javascript.java

public class Javascript {   
    Context cont;
    WebView webView;

    Javascript(Context c, WebView w) {
        cont = c;
        webView = w;
    }

    // function called in the javascript by AndroidFunction.test();
    public void test() {
          // Breaking point!!!
        webView.loadUrl("javascript:helloBack()");
    }

Error:

错误:

03-24 11:47:50.103: W/WebView(21026):   at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
03-24 11:47:50.103: W/WebView(21026):   java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper{41ab68f8} called on Looper{41bb70a8}, FYI main Looper is Looper{41ab68f8})

03-24 11:47:50.103: W/WebView(21026):   at android.webkit.WebView.checkThread(WebView.java:2063)
03-24 11:47:50.103: W/WebView(21026):   at android.webkit.WebView.loadUrl(WebView.java:794)
03-24 11:47:50.103: W/WebView(21026):   at com.example.hellobt.Javascript.test(Javascript.java:24)

03-24 11:47:50.103: W/WebView(21026):   at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
03-24 11:47:50.103: W/WebView(21026):   at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
03-24 11:47:50.103: W/WebView(21026):   at android.os.Handler.dispatchMessage(Handler.java:102)

03-24 11:47:50.103: W/WebView(21026):   at android.os.Looper.loop(Looper.java:137)
03-24 11:47:50.103: W/WebView(21026):   at android.os.HandlerThread.run(HandlerThread.java:61)

Thanks for the answer. I edited the function in my Javascript file like this:

谢谢你的回答。我在我的 Javascript 文件中编辑了这个函数,如下所示:

private void test(final String s) {
        webView.post(new Runnable() {
            public void run() {
                webView.loadUrl("javascript:" + s + ";");
            }
        });
        System.out.println("javscript done..");
    }

回答by ksasq

The JavaScript method is executed on a background (i.e. non-UI) thread. You need to call all Android View related methods on the UI thread. You can achieve what you need with:

JavaScript 方法在后台(即非 UI)线程上执行。您需要在 UI 线程上调用所有与 Android View 相关的方法。您可以通过以下方式实现您的需求:

mWebView.post(new Runnable() {
    @Override
    public void run() {
        mWebView.loadUrl(...).
    }
});

Which will post the task to run on the UI thread.

这将发布在 UI 线程上运行的任务。

回答by CoolMind

In my case nothing was shown in a WebView, so I prefer another way:

就我而言,WebView 中没有显示任何内容,所以我更喜欢另一种方式:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        final WebView webView = (WebView) findViewById(R.id.map);
        webView.loadDataWithBaseURL(...);
    }
});

回答by Akhil Ghatiki

This can be come over by using the post method. Please go through below code.

这可以通过使用 post 方法来解决。请通过下面的代码。

 m_targetView.post(new Runnable() {
                        @Override
                        public void run() {
                            m_targetView.loadUrl(".....");
                        }
                    });

回答by Roman

Javaversion: You must to use Runnableinterface and Post to Handler.

Java版本:您必须使用Runnable接口和 Post to Handler

webView.post(new Runnable() {
          @Override
          public void run() {
             webView.loadUrl("file:///android_asset/www/index.html");
          }
       });

Kotlinversion:

科特林版本:

webView.post(new Runnable {
   webView.loadUrl("file:///android_asset/www/index.html")
})