【问题标题】:tImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its viewstImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图
【发布时间】:2013-07-17 12:56:50
【问题描述】:
07-17 15:51:16.429: D/visibility(10457): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-17 15:51:21.339: D/visibility(10457): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-17 15:51:32.309: D/visibility(10457): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-17 15:51:40.604: D/visibility(10457): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

我的代码:

try {
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json = jArray.getJSONObject(0);
                    String jsona = json.getString("Email");
                    if (jsona == null) {
                        goodrating.setVisibility(View.VISIBLE);
                        badrating.setVisibility(View.VISIBLE);
                    } else {
                        goodrating.setVisibility(View.GONE);
                        badrating.setVisibility(View.GONE);
                    }
                }

            } catch (Exception e) {
                Log.d("visibility", e.toString());
            }

这些代码在 ASYNCTASK DOINBACKGROUND 中 那为什么我会收到这个错误?

【问题讨论】:

    标签: android json android-asynctask


    【解决方案1】:

    尝试将您的代码更改为:

    try {
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json = jArray.getJSONObject(0);
                String jsona = json.getString("Email");
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        if (jsona == null) {
                            goodrating.setVisibility(View.VISIBLE);
                            badrating.setVisibility(View.VISIBLE);
                        } else {
                            goodrating.setVisibility(View.GONE);
                            badrating.setVisibility(View.GONE);
                        }
                    }
                });
            }
    
        } catch (Exception e) {
            Log.d("visibility", e.toString());
        }
    

    【讨论】:

    • 为什么需要两个runOnUIThread?一个就足够了,您可以检查其中的条件。
    • @Raghunandan 是的,也可以。
    • 您应该改用 publishProgress。这就是它的用途。
    【解决方案2】:

    看起来您正在从后台线程更新 ui,这是不可能的。您必须在 ui 线程上更新 ui。使用onPostexecutedoInbackground 计算的结果是 onPostExecute 的参数。所以在doInbackground 中返回结果。根据onPostExecute中返回的值更新ui。

    http://developer.android.com/reference/android/os/AsyncTask.html

    查看上述链接中4个步骤下的主题

    您也可以使用runOnUiThread 来更新ui(更改可见性)。

        @Override
        protected String doInBackground(Void ... params) {
    
        return jsona ;
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
                 if (result == null) {
                        goodrating.setVisibility(View.VISIBLE);
                        badrating.setVisibility(View.VISIBLE);
                    } else {
                        goodrating.setVisibility(View.GONE);
                        badrating.setVisibility(View.GONE);
                    }
    }
    

    【讨论】:

    • 可能会改变视图的可见性?
    【解决方案3】:

    你必须在

    中触动你的观点
     protected void onPostExecute(Long result) {
             // Touch your views
         }
    

    【讨论】:

      【解决方案4】:

      您正在尝试从 doInBackground() 方法中的后台线程访问 UI 组件(视图)。你不能这样做参考这个Link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-21
        相关资源
        最近更新 更多