Can't create handler inside thread that has not called Looper.prepare()解决办法
2014-05-05 21:50
519 查看
参考:转: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的功能
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 } |
相关文章推荐
- 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()
- 解决bug:运行项目时报异常 “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
- Can't create handler inside thread that has not called Looper.prepare() 错误有关问题
- Android : 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()
- 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
- Unity3d for android 访问UI控件报错 Can\'t create handler inside thread that has not called Looper.prepare()
- Android在Activity中启动一个新进程报错:Can't create handler inside thread that has not called Looper.prepare()。
- Can’t create handler inside thread that has not called Looper.prepare()
- 在子线程中new 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()