android与webview js交互问题
@JavascriptInterface
public void goToUrl(String url){
now.loadUrl("file:///android_asset/loadingfailed.html");
}
js交互调用上方法时出现如下错误
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) {c44f280} called on Looper
(JavaBridge, tid 26651) {a2bc55}, FYI main Looper
is Looper (main, tid 1) {c44f280})
解决:
@JavascriptInterface
public void goToUrl(String url){
//fragment 使用getActivity()
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
now.loadUrl("file:///android_asset/loadingfailed.html");
}
});
}
新版的Android的SDK要求在创建WebView所在的线程中操作