WebView methods on same thread error WebView methods on same thread error android android

WebView methods on same thread error


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.


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(...);    }});


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")})