解决java.lang.IllegalStateException: Calling View methods on another thread than the UI thread

时间:2023-02-10 20:57:19

1. 解决WebView当中的UserAgent的问题


在android4.4+版本中,webview的使用发生了一些改变:

见文档:http://developer.android.com/intl/zh-cn/guide/webapps/migrating.html

我们的工程中要收集userAgent,在4.4之后就不能在主线程new webview了,因此使用文档中提供的方法:

If you need to retrieve the user agent but don't need to store it for your app or do not want to instantiateWebView, you should use the static method, getDefaultUserAgent(). However, if you intend to override the user agent string in your WebView, you may instead want to use getUserAgentString().


代码如下:

[java] view plain copy
  1. public static String getUserAgent(Context ctx) {  
  2.     //api 19 之前  
  3.     if(Build.VERSION.SDK_INT < 19){  
  4.         WebView webview = new WebView(ctx);  
  5.         WebSettings settings = webview.getSettings();  
  6.         return settings.getUserAgentString();  
  7.     }  
  8.       
  9.     //api >=19  
  10.     return WebSettings.getDefaultUserAgent(ctx);   

2. 解决webView显示列表数据并分享到新浪 微信 朋友圈


分享接口是调用原生接口的:

    /** * 分享 * * @param sns_Json 分享SnsShareEntity实体 --> json格式 */
    @JavascriptInterface
    public void shareGood(String sns_Json) {
        Logger.e("分享 :"+sns_Json);
        final SnsShareEntity snsShareEntity = GsonUtil.GetFromJson(sns_Json, SnsShareEntity.class);
        if (snsShareEntity != null) {
        //调用Umeng的分享接口
        initShare();
        ShareUtils.sendVideoShare(context, topBar, new GoodShareInterFace(mController, snsShareEntity, context, ""));
       

webView中直接调用shareGood方法,分享到微信 微信朋友圈、QQ都是没有问题,唯独分享到新浪微博的是报错:
解决java.lang.IllegalStateException: Calling View methods on another thread than the UI thread

根据提示和结合自己的项目分析:
webview调用shareGood方法,本身是属于webview里js调用,不属于UiThread;
当分享新浪微博的时候,新浪微博是打开一个网页登录界面的。所以需要运行在UiThread中,导致报错。

最后在*中搜索出一个解决方案:
java.lang.IllegalStateException: Calling View methods on another thread than the UI thread

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // Code for WebView goes here
    }
});


// This code is BAD and will block the UI thread
webView.loadUrl("javascript:fn()");
while(result == null) {
  Thread.sleep(100);
}

所以最后我自己修改了shareGood方法,如下:

    /** * 分享 * * @param sns_Json 分享SnsShareEntity实体 --> json格式 */
    @JavascriptInterface
    public void shareGood(String sns_Json) {
        Logger.e("分享 :"+sns_Json);
        final SnsShareEntity snsShareEntity = GsonUtil.GetFromJson(sns_Json, SnsShareEntity.class);
        if (snsShareEntity != null) {
            //解决Calling View methods on another thread than the UI thread.错误
// topBar.postDelayed(new Runnable() {
// @Override
// public void run() {
// // Code for WebView goes here
// initShare();
// ShareUtils.sendVideoShare(context, topBar, new GoodShareInterFace(mController, snsShareEntity, context, ""));
// }
// },1000);

            //解决Calling View methods on another thread than the UI thread.错误
            //http://*.com/questions/20255920/java-lang-illegalstateexception-calling-view-methods-on-another-thread-than-the
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // Code for WebView goes here
                    initShare();
                    ShareUtils.sendVideoShare(context, topBar, new GoodShareInterFace(mController, snsShareEntity, context, ""));
                }
            });
        }
    }

这篇文章希望能够帮助大家,有任何问题,请随时留言,我会尽快回复大家,谢谢!