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

本文记录了一次在Android应用开发中遇到的UI线程错误,即非主线程尝试更新UI的问题,并给出了通过Handler实现异步更新UI的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天继续在编写app,后来女朋友来消息,按了home将app后台运行,再打开时崩了,

,报了如下错误:

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











                
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值