104

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

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)

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

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

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:

private void test(final String s) {
        webView.post(new Runnable() {
            public void run() {
                webView.loadUrl("javascript:" + s + ";");
            }
        });
        System.out.println("javscript done..");
    }
1
  • In your edit you mention that you've changed your code based on (one of) the answer(s), making it sound like this still hasn't solved your problem - but there is an accepted answer. I suggest you make it explicit whether this solved the problem or not (or otherwise just undo the edit).
    – Tom
    Sep 6, 2020 at 8:07

4 Answers 4

245

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.

6
  • where did you add the snippet (mWebView) to your original code?
    – Bowie
    May 28, 2014 at 3:04
  • In the test() function, see my above question. Aug 26, 2014 at 13:32
  • What if you need to return a value? eg return webview.getUrl? stackoverflow.com/questions/29748782/… @JohanHoeksma Apr 20, 2015 at 13:26
  • Do you know how and when this problem occurred? In my Nexus 5X(Android 7.1.1), it's ok, but some devices crashed.
    – Ellie Zou
    Dec 31, 2016 at 9:18
  • how to pass a variable? I tried to add variable in mWebView.loadUrl("javascript:test('" + MyData + "');"); but the value never get passed to javascript ! Dec 16, 2017 at 7:29
18

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(...);
    }
});
2
  • what is "runOnUiThread" please? May 11, 2022 at 16:31
  • @Martian2049, it is a method of Activity to run in UI thread.
    – CoolMind
    May 11, 2022 at 19:10
10

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.