0
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

4 回答 4

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());
    }
于 2013-07-17T13:07:04.350 回答
1

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

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);
                }
}
于 2013-07-17T12:59:47.640 回答
0

你必须触及你的观点

 protected void onPostExecute(Long result) {
         // Touch your views
     }
于 2013-07-17T12:58:16.107 回答
0

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

于 2013-07-17T13:00:04.813 回答