一些簡單的原因如: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 返回的結果
}
});
}
}
});