Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
来源:互联网 发布:淘宝云标签考试 编辑:程序博客网 时间:2024/06/04 19:24
http://www.luchg.com/blog/showArticle_123.html
当我们在一个新的线程中使用android UI时,遇到这个异常
看了很多文章也还是不太清楚具体是什么原因,但是处理这个问题是很简单的,如下代码:
@Override
public
void
run() {
//前面代码省略
Looper.prepare();
Log.i(
"---my tag---"
,
"下载完成:"
+downloadPath);
Toast.makeText(context, fileName+
"下载完成..."
, Toast.LENGTH_LONG).show();
Looper.loop();
}
在使用Toast之前加一行Looper.prepare();,然后在Toast之后加一行Looper.loop();,即可实现。看了很多文章也没理解,所以这里就不误导大家了,感兴趣的自己搜搜原因。
http://songyuanlin1101.blog.163.com/blog/static/31126387201342951759904/
E/AndroidRuntime(7200): Uncaught handler: thread Thread-8 exiting due to uncaught exception
E/AndroidRuntime( 7200): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。
解决办法:
public void sendJson2Server(final JSONObject mJs) {
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
PushServiceEx.sendPublic(mTopicServer, mJs.toString());
System.out.println(mJs.toString());
}
};
mHandler.sendEmptyMessageDelayed(0, 5000);
// PushServiceEx.sendPublic(mTopicServer, mJs.toString());
}
new Thread() {
public void run() {
Looper.prepare();
mPst.startPushService();
mPst.sendJson2Server(qJson);//上线发消息给server
Looper.loop();
}
}.start();
加上上面红色两行。
- Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- Android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- Android 异常 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper
- Android java.lang.RuntimeException: Can\'t create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- IO之转换流与重定向标准输入/输出
- c/c++如何获得数组的长度
- 关于实仓与虚仓和调拨单和虚仓调拨单的区别
- awk命令学习
- android 弹出多选对话框
- Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- wikioi-天梯-进入省队-线段树-1282:约瑟夫问题
- Sitemesh入门
- ClientCnxn: Session 0x0 for server null, unexpected error 异常解决
- C#学习笔记:数据库编程1
- 从extenionplugin的C++ 模块中读取数据并显示到Qt的ListView上
- WIN7上搭建Windows Phone 8 开发环境——VMware Workstation下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法
- NoSQL数据库的35个应用场景
- 自己动手写分词引擎——逆向最大、正向最大、双向最大分词算法的实现