java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解...

本文介绍了解决Android开发中出现的java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()这一异常的方法。该异常通常发生在尝试在非UI线程中显示Toast的情况下。文章提供了一个使用Handler机制来确保在正确线程中显示Toast的解决方案。

代码改变世界

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法

 

android Toast提示异常:java.lang.RuntimeException: Can't create handler inside thread that has not called

原因是在子线程弹Toast了, 切记,Toast只能在UI线程弹出

 

解决办法:

new Thread(new Runnable() {  
              
            @Override  
            public void run() {  
                // TODO Auto-generated method stub  
                  
                try {  
                    Thread.sleep(1000);  
                    handler.sendEmptyMessage(0);  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                  
            }  
        }).start();

  

private Handler handler = new Handler() {  
  
        @Override  
        public void handleMessage(Message msg) {
      Toast.makeText(CommodityDetails.this, "修改失败!", Toast.LENGTH_SHORT).show();
} };

  

完美解决!!!

转载于:https://www.cnblogs.com/shmilyGWT/p/8087796.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值