android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi

오늘 계속해서 앱을 작성하고 있는데 여자친구가 연락이 와서 홈을 누르면 앱을 백엔드에서 실행하고 다시 켜면 붕괴합니다.
, 다음 오류를 보고했습니다.
 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
대체적으로, 나는 비주선 라인에서 ui를 업데이트해서 붕괴되었다
(정말 저급한 실수다. 작년에 저지른 실수를 올해도 저질렀다)
 
  
private  void setdate() {
    new Thread(new Runnable() {
        @Override

        public void run() {
            String strURL = "http://jirenguapi.applinzi.com/weather.php";
            URL url = null;
            try {
                url = new URL(strURL);

                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                InputStreamReader input = new InputStreamReader(httpConn
                        .getInputStream(), "utf-8");
                BufferedReader bufReader = new BufferedReader(input);
                String line = "";
                StringBuilder contentBuf = new StringBuilder();
                while ((line = bufReader.readLine()) != null) {
                    contentBuf.append(line);
                }
                date_weather.setText(contentBuf);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

            }

        }
    }).start();
}


 
  
 
   
  

, handler , , handler, ui

 
  
private Handler 
handler_setdate =  new Handler()
{
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        String message = msg.obj.toString();
        date_weather.setText(message);


    }

};
private  void setdate() {
    new Thread(new Runnable() {
        @Override

        public void run() {
            String strURL = "http://jirenguapi.applinzi.com/weather.php";
            URL url = null;
            try {
                url = new URL(strURL);

                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                InputStreamReader input = new InputStreamReader(httpConn
                        .getInputStream(), "utf-8");
                BufferedReader bufReader = new BufferedReader(input);
                String line = "";
                StringBuilder contentBuf = new StringBuilder();
                while ((line = bufReader.readLine()) != null) {
                    contentBuf.append(line);
                }
                Message msg = new Message();
                msg.obj= contentBuf;
                handler_setdate.sendMessage(msg);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

            }

        }
    }).start();
}


 
  
 
  
 
   
  
 
  
 
  
 
  
 
  
 
 

좋은 웹페이지 즐겨찾기