一些简单的原因如:js接口没开、方法名拼写错误等不做过多赘述。
本片博客主要研究以下报错原因:
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {4186a978} called on Looper (JavaBridge, tid 385) {41a24830}, FYI main Looper is Looper (main, tid 1) {4186a978})
字面翻译就是:你没在主线程操作webview,报错
只要把操作webview的代码放在UI线程里面执行就可以了:
runOnUiThread(new Runnable() {
@Override
public void run() {
if (version < 18) {
show("low");
wv.loadUrl("javascript:goMy()");
} else {
show("high");
wv.evaluateJavascript("javascript:goMy()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
//此处为 js 返回的结果
}
});
}
}
});