WebView methods with the same stream error

I have a program for Android (Java + html in webview). I can call from javascript to Java code. But on the contrary, stopped (after the update in the eclipse).

So this is what I'm trying to do

  • Make web browsing (worked)
  • javascript call for AndroidFunction.test (); (Worked)
  • function call java test () webView.loadUrl ("javascript: helloBack ()"); (! does not work any more)

I tried to let it work with WebView in MainActivity, but it did not 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()"); } 

Mistake:

 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 as follows:

 private void test(final String s) { webView.post(new Runnable() { public void run() { webView.loadUrl("javascript:" + s + ";"); } }); System.out.println("javscript done.."); } 
+47
javascript android android-webview
Mar 24 '14 at 11:07
source share
3 answers

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

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

A job will be sent to run in the user interface thread.

+137
Mar 24 '14 at 13:36
source share

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

 runOnUiThread(new Runnable() { @Override public void run() { final WebView webView = (WebView) findViewById(R.id.map); webView.loadDataWithBaseURL(...); } }); 
+7
Apr 08 '16 at 9:06
source share

This can be achieved using the post method. Please go below the code.

  m_targetView.post(new Runnable() { @Override public void run() { m_targetView.loadUrl("....."); } }); 
+2
Mar 07 '17 at 12:22
source share



All Articles