maomingchao

android 平台 java和javascript 通信问题 A WebView method was called on thread 'JavaBridge'.

 

  

java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. 
All WebView methods must be called on the same thread


之前的 android4.4版本的代码跑没有问题,到了5.0出了这个问题.查询百度.解决方法:
//4.4之前版本调用函数
public void sendToJs(){
try {
            Map map = new HashMap();
            map.put("serial", android.os.Build.SERIAL);
            JSONObject jsonMsg = new JSONObject(map);
            String strJson = "javascript:Tojs('" + jsonMsg.toString() + "')";
            mWebView.loadUrl(strJson);
        }
        catch (Exception e) {
            Toast.makeText(getApplicationContext(), "getInfo error", Toast.LENGTH_SHORT).show();
        }
}

//android 5.0 版本使用方法:

public void sendToJs(){
mWebView.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Map map = new HashMap();                  
                    map.put("serial", android.os.Build.SERIAL);
                    JSONObject jsonMsg = new JSONObject(map);
                    String strJson = "javascript:Tojs('" + jsonMsg.toString() + "')";
                    mWebView.loadUrl(strJson);
                }
                catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "getInfo error", Toast.LENGTH_SHORT).show();
                }
            }
        });
}

  

  




posted on 2017-12-08 09:59  maomingchao  阅读(1292)  评论(0)    收藏  举报

导航