同一线程错误的 WebView 方法

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

这就是我要做的

  • 制作一个 webview (工作)
  • 调用 javascript 到 AndroidFunction.test () ; (工作)
  • Java test ()函数调用 webView.loadUrl (“ javascript: helloBack ()”) ; (! 不再工作)

我尝试让它在 MainActivity 中与 WebView 一起工作,但是它不能工作。

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

错误:

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)

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

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

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

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

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