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 に答える