在Async中使用Toast
2015-12-15 23:38
232 查看
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
在报错的方法前加Looper.prepare();
方法末尾加Looper.loop();
在async里面调用toast时候 会报错 toast前后加就可以解决
但是问题来了 loop是一个循环 如果是你要在toast后面还要执行 这种方式就不可行
你需要这样
public void toast(final String s)
{
Handler handler=new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
Toast.makeText(context,s,Toast.LENGTH_SHORT).show();
}
}
);
使用handler发送一个消息 然后在async中调用 toast(str); 就可以解决了
在报错的方法前加Looper.prepare();
方法末尾加Looper.loop();
在async里面调用toast时候 会报错 toast前后加就可以解决
但是问题来了 loop是一个循环 如果是你要在toast后面还要执行 这种方式就不可行
你需要这样
public void toast(final String s)
{
Handler handler=new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
Toast.makeText(context,s,Toast.LENGTH_SHORT).show();
}
}
);
使用handler发送一个消息 然后在async中调用 toast(str); 就可以解决了
相关文章推荐
- java面试题
- 第三个Sprint冲刺第八天
- make menuconfig错误的解决办法
- 当编译VC项目时提示不能打开生成的动态库.dll或者.exe文件
- 安卓sqlite数据库中sql语句的问题
- Eclipse 修改web项目名称
- 延迟是AR/VR体验的基础
- 设计模式笔记一:简单工厂模式
- 如何评估一个外链的价值
- 10007---CSS创建
- 【Python 第8课】while
- hdu 1281 (二分图匹配)
- sql server中区分大小写问题
- leetcode Longest Valid Parentheses python
- hiho 20 使用线段树进行区间修改
- 支付验签模拟
- LVCOLUMN
- 快速排序
- 项目分享二:APP 小红点中数字的处理
- 包装类型的比较,如:Integer,Long,Double