为什么子线程不能更新Toas
2016-03-01 21:49
183 查看
代码: 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之间的关系
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之间的关系
相关文章推荐
- 3月1日,新细节
- 决策树笔记:使用ID3算法
- 年终总结
- GUI界面设计1 三角函数
- [置顶] Unity基于NGUI实现拖拽功能
- 8. Smarty 基本语法
- Runtime.exec使用错误导致延迟.md
- Unity学习笔记——利用脚本实现对一个物体的第三人称观察
- activity的横竖屏切换
- A different object with the same identifier value was already associated with the session
- Leetcode总结,“公式化”二分法
- 33. 高精度练习之乘法
- 正则表达式
- 安卓中制作音乐播放器中的通知栏遇到的几个问题
- 33. 高精度练习之乘法
- 33. 高精度练习之乘法
- 33. 高精度练习之乘法
- linux命令详解
- Python3.5 Django1.9.2常用命令
- System.out.println内容写入txt