Can't create handler inside thread that has not called Looper.prepare()解决办法
来源:互联网 发布:c 高级编程第10版下载 编辑:程序博客网 时间:2024/06/06 12:27
参考:转:Thread和Looper以及Handler和Message详解 Android开发必读
像提示说的,添加 Looper.prepare();
Thread action=newThread() {
publicvoidrun() {
Looper.prepare();
todo();
Looper.loop(); } }; action.start()
解决办法二:使用handler
参考:http://mycoding.javaeye.com/blog/882756
在主activity中定一个Handler的成员,然后实现handlemassage函数,创建线程后在runable的run函数里new一个message,然后指定message对象的what成员,这个是指定message的一个id,然后在run中调用Handler的成员,使用其成员方法中的sendmessage(好像是叫这个),handlemassage函数中参数有个massage,根据该message参数中的what来对你发送message时指定的what来处理UI的功能
privateHandler mHandler=newHandler(){
publicvoidhandleMessage(Message msg) {
switch(msg.what)
{
caseID_USER:
//获取传递的数据//Bundle data = msg.getData();
//int count = data.getInt("COUNT");
//处理UI更新等操作
} };
};
主activity中创建线程
Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->
MyThread thread=newMyThread();
mThread=newThread(thread);
mThread.start();
MyThread
Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->
classMyThreadimplementsRunnable {
publicvoidrun() {
//执行数据操作,不涉及到UI
Message msg=newMessage();
msg.what=ID_USER;
//这三句可以传递数据
//Bundle data = new Bundle();
//data.putInt("COUNT", 100);//COUNT是标签,handleMessage中使用
//msg.setData(data);
mHandler.sendMessage(msg); //向Handler发送消息,更新UI
}
0 0
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- 图片下载时遇到Can't create handler inside thread that has not called Looper.prepare()解决办法
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- 异常:Can't create handler inside thread that has not called Looper.prepare()解决办法
- 解决Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask
- Handler——Can't create handler inside thread that has not called Looper.prepare()
- Handler机制和Can't create handler inside thread that has not called Looper.prepare()异常
- Can't create handler inside thread that has not called Looper.prepare()错误的解决
- Can't create handler inside thread that has not called Looper.prepare() 异常
- Can't create handler inside thread that has not called Looper.prepare()
- Can’t create handler inside thread that has not called Looper.prepare()
- Can’t create handler inside thread that has not called Looper.prepare()
- Can't create handler inside thread that has not called Looper.prepare() 终极解决方法
- Java高级部分笔记
- 今生得到你是我最大的幸福,如果有來生,我還希望能娶妳
- java压缩文件工具类
- 找实习经历——盘点各大公司的拒信
- HDU 1890 Robotic Sort 单点查找区间翻转 Splay裸题
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- 教你走出“事情多”的忙乱困局
- map
- HDU 1847 Good Luck in CET-4 Everybody!
- libgdx编译的时候报错
- 关于“warning C4786”
- 分享一个jquery easyui拼接table的例子
- RAR和ZIP:压缩大战真相
- 仿常见应用的下拉刷新,上拉加载的控件实现原理肉,例Android-PullToRefresh