Android - A WebView method was called on thread 'JavaBridge
源码:
webView.evaluateJavascript("javascript:"+callBack + "(" + request + ")", new ValueCallback<String>() { @Override public void onReceiveValue(String value) { Log.d(TAG, "onReceiveValue: "+value+ pin); // SoftTokenUtil.getInstance().showDialogSoftTokenSuccess(mBaseActivity); } });
错误: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) {8e3ea1f} called on Looper (JavaBridge, tid 2358) {b82a469}, FYI main Looper is Looper (main, tid 1) {8e3ea1f})at android.webkit.WebView.checkThread(WebView.java:2340)at android.webkit.WebView.evaluateJavascript(WebView.java:1042)
解决方案:
在外面一层包上webView.post()方法。
webView.post(new Runnable() { @Override public void run() { String request = "{'type':'CHALLENGE_OTP', 'mode':'ONLINE', 'challengeOtp':'"+pin+"'}"; Log.d(TAG, "run: "+ "javascript:"+callBack + "(" + request + ")"); webView.evaluateJavascript("javascript:"+callBack + "(" + request + ")", new ValueCallback<String>() { @Override public void onReceiveValue(String value) { Log.d(TAG, "onReceiveValue: "+value+ pin); } }); } });