WebView methods on same thread error

后端 未结 4 1269
南方客
南方客 2020-12-02 11:44

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).

相关标签:
4条回答
  • 2020-12-02 12:15

    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:

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

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

    0 讨论(0)
  • 2020-12-02 12:23

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

     m_targetView.post(new Runnable() {
                            @Override
                            public void run() {
                                m_targetView.loadUrl(".....");
                            }
                        });
    
    0 讨论(0)
  • 2020-12-02 12:26

    Java version: You must to use Runnable interface and Post to Handler.

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

    Kotlin version:

    webView.post(new Runnable {
       webView.loadUrl("file:///android_asset/www/index.html")
    })
    
    0 讨论(0)
  • 2020-12-02 12:27

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

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            final WebView webView = (WebView) findViewById(R.id.map);
            webView.loadDataWithBaseURL(...);
        }
    });
    
    0 讨论(0)
提交回复
热议问题