안녕하세요 안드로이드 listview 질문입니다!


안녕하세요 이렇게 질문을 올리게 됩니다. 밑에 소스는 listview에서 클릭 이벤트를 받아오게 되면 나오는 화면 소스입니다 listview 클릭시 모든 화면은 똑같은 프레임이구요 json에서 postion값을 받아와서 하나씩 넣어주는 형식입니다. 밑에 소스를 짜보았는데요 무엇이 문제인지 잘 몰라서 올리게 됩니다. log와 소스 한번만 봐주실수 있을까요? 저도 너무 막연해서 이렇게 올리게 됩니다. 계속 업데이트 상황 보고 있겠습니다. 알려주시면 감사하겠습니다!

public class store_space extends AppCompatActivity {

    private ArrayList<store_itemInfo> results = new ArrayList<>();
    private int mPosition = 0;
    private TextView source, title, address, explain;
    private ImageView imageview, imageview1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_space);


        mPosition = getIntent().getIntExtra("position", 0);
        source = (TextView) findViewById(R.id.store_space_Text_Source);
        title = (TextView) findViewById(R.id.store_space_Text_Title);
        address = (TextView) findViewById(R.id.store_space_Text_Address);
        explain = (TextView) findViewById(R.id.store_space_Text_Explain);
        imageview = (ImageView) findViewById(R.id.store_space_ImageView);
        imageview1 = (ImageView) findViewById(R.id.store_space_ImageView1);
        new AsyncHttpTask().execute();
//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
    }

    public class AsyncHttpTask extends AsyncTask<String, Void, String> {
        HttpURLConnection urlConnection;
        StringBuilder result;
        ProgressDialog asyncDialog = new ProgressDialog(
                store_space.this);

        @Override
        protected void onPreExecute() {
            asyncDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            asyncDialog.setMessage("로딩중입니다..");

            // show dialog
            asyncDialog.show();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            result = new StringBuilder();
            urlConnection = null;

            try {
                URL url = new URL("https://script.google.com/macros/s/AKfycby39ckxtMJ_fupsVFO2-AueX1eImiMA-C25srnn0WTKoNJlPKaW/exec");
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line;
                String result = "";
                while ((line = reader.readLine()) != null) {
                    result += line;
                }
                parseResult(result);
                // Close stream
                if (null != in) {
                    in.close();
                }
                return result;

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                assert urlConnection != null;
                urlConnection.disconnect();
            }
            return result.toString();
        }


        @Override
        protected void onPostExecute(String result) {
            // Download complete. Lets update UI
            parseResult(result);
            asyncDialog.dismiss();
        }

        private void parseResult(String result) {
            try {
                if (null != results) {
                    results = new ArrayList<store_itemInfo>();
                }
                JSONArray response = new JSONArray(result);
                for (int i = 0; i < response.length(); i++) {
                    store_itemInfo temp = new store_itemInfo();
                    temp.setTitle(response.getJSONObject(i).getString("title"));
                    temp.setImage(response.getJSONObject(i).getString("image"));
                    temp.setImage1(response.getJSONObject(i).getString("image1"));
                    temp.setPosition(response.getJSONObject(i).getInt("position"));
                    temp.setAddress(response.getJSONObject(i).getString("address"));
                    temp.setExplain(response.getJSONObject(i).getString("explain"));
                    temp.setSource(response.getJSONObject(i).getString("source"));
                    source.setText(temp.getSource());
                    title.setText(temp.getTitle());
                    address.setText(temp.getAddress());
                    explain.setText(temp.getExplain());
                    Picasso.with(store_space.this).load(temp.getImage()).fit().into(imageview);
                    Picasso.with(store_space.this).load(temp.getImage1()).fit().into(imageview1);
                    if (mPosition == temp.getPosition())
                        results.add(temp);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

log화면입니다

05-30 17:34:35.047 23188-24324/org.androidtown.ezcoffee W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-30 17:34:35.050 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6378)
05-30 17:34:35.050 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:926)
05-30 17:34:35.050 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4640)
05-30 17:34:35.050 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.view.View.invalidateInternal(View.java:11731)
05-30 17:34:35.063 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.view.View.invalidate(View.java:11695)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.view.View.invalidate(View.java:11679)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.widget.TextView.checkForRelayout(TextView.java:7049)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.widget.TextView.setText(TextView.java:4189)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.widget.TextView.setText(TextView.java:4047)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.widget.TextView.setText(TextView.java:4002)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.store.store_space$AsyncHttpTask.parseResult(store_space.java:126)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.store.store_space$AsyncHttpTask.doInBackground(store_space.java:87)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.store.store_space$AsyncHttpTask.doInBackground(store_space.java:55)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-30 17:34:35.065 23188-24324/org.androidtown.ezcoffee W/System.err:     at java.lang.Thread.run(Thread.java:818)
05-30 17:34:35.073 23188-23188/org.androidtown.ezcoffee W/System.err: org.json.JSONException: End of input at character 0 of 
05-30 17:34:35.073 23188-23188/org.androidtown.ezcoffee W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
05-30 17:34:35.073 23188-23188/org.androidtown.ezcoffee W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
05-30 17:34:35.073 23188-23188/org.androidtown.ezcoffee W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:92)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.store.store_space$AsyncHttpTask.parseResult(store_space.java:116)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.store.store_space$AsyncHttpTask.onPostExecute(store_space.java:107)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.store.store_space$AsyncHttpTask.onPostExecute(store_space.java:55)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:632)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at android.os.Looper.loop(Looper.java:135)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5356)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
05-30 17:34:35.074 23188-23188/org.androidtown.ezcoffee W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

매번 감사합니다!!!

  • 2016년 05월 30일에 작성됨
    취준생..ㅠ_ㅠ 안드로이드요

조회수 393


1 답변


좋아요
1
싫어요
채택취소하기

doInBackground()에서 parseResult()를 호출하면서 발생하는 에러입니다. (정확히는 parseResult() 내부에서 TextView의 setText를 호출하는 부분) doInBackground()는 백그라운드 쓰레드에서 수행되는 함수이기 때문에 이곳에서 UI 업데이트를 할 경우, android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views 익셉션이 발생합니다.

현재 코드에서 doInBackground의 parseResult()를 호출하지 않아도 제대로 동작할 것 같은데 이 부분을 제거한 후 테스트 해보세요.


  • 감사합니다!! 그게 왜 들어가있었을까요 ㅠㅠㅠㅠ 하 감사합니다 정말로 덕분에 잘 해결 되었어요 나머지는 일단 json이 들어옴에 감사합니다~! 좋은 하루 되세요    한정희   2016.5.30 20:00     

로그인이 필요한 기능입니다.

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 작성한 답변에 다른 개발자들이 댓글을 작성하거나 좋아요/싫어요를 할 수 있기 때문에 계정을 필요로 합니다.
► 로그인
► 계정만들기
Close