为什么子线程不能更新Toas

来源:互联网 发布:python主要应用领域 编辑:程序博客网 时间:2024/04/28 22:39
代码: new Thread() {
public void run() {
try {

Toast.makeText(context, checkBean.getMsg(), 1).show();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};

}.start();

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

原因:Toast中用到了Handler,而子线程中并没有初始化Looper,就不能调用Handler的当前的Looper对象了。


解决办法: Looper.prepare();
Toast.makeText(context, checkBean.getMsg(), 1).show();
Looper.loop();

探究:Handler与Looper之间的关系

0 0
原创粉丝点击