A WebView method was called on thread 'JavaBridge'. All WebView methods must be called

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所在的线程中操作
--------------------- 
作者:过客Diane 
来源:CSDN 
原文:https://blog.csdn.net/qiwei352/article/details/81506851 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Android,Fragment,刷新)