tImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图

时间:2013-07-17 12:56:51

标签: android json android-asynctask

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 那我为什么会收到这个错误?

4 个答案:

答案 0 :(得分:2)

尝试将代码更改为:

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());
    }

答案 1 :(得分:1)

看起来你正在从后台线程更新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);
                }
}

答案 2 :(得分:0)

您必须在

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

答案 3 :(得分:0)

您正尝试在doInBackground()方法中从您的案例中的后台线程访问UI组件(View)。我们不允许您这样做Link