Android - A WebView method was called on thread 'JavaBridge

Posted 敢于尝试

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
});
}
});

































以上是关于Android - A WebView method was called on thread 'JavaBridge的主要内容,如果未能解决你的问题,请参考以下文章

Android:控件WebView显示网页

Android中webView的基础使用

获取Android webview的点击元素

Android WebView填坑记录

android WebView 截取长图

WebView加载页面