Can't create handler inside thread that has not called Looper.prepare()解决办法
来源:互联网 发布:网络教育的本科学历国家承认吗 编辑:程序博客网 时间:2024/05/29 15:57
链接:http://blog.csdn.net/a7a9a68/article/details/7841762
- private Handler mHandler = new Handler(){
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case ID_USER:
- //获取传递的数据
- //Bundle data = msg.getData();
- //int count = data.getInt("COUNT");
- //处理UI更新等操作
- }
- };
- };
只要在run()方法中加入 Looper.prepare()就可以解决问题了,但是出现这种问题的原因是因为Android中不能在子线程中来刷新UI线程。建议是在你的子线程中添加hander来发送消息更新线程。
- private Handler mHandler = new Handler(){
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case ID_USER:
- //获取传递的数据
- //Bundle data = msg.getData();
- //int count = data.getInt("COUNT");
- //处理UI更新等操作
- }
- };
- };
主activity中创建线程
- MyThread thread = new MyThread();
- mThread = new Thread(thread);
- mThread.start();
MyThread
- class MyThread implements Runnable {
- public void run() {
- //执行数据操作,不涉及到UI
- Message msg = new Message();
- msg.what = ID_USER;
- //这三句可以传递数据
- // Bundle data = new Bundle();
- // data.putInt("COUNT", 100);//COUNT是标签,handleMessage中使用
- // msg.setData(data);
- mHandler.sendMessage(msg); // 向Handler发送消息,更新UI
- }
通过一个Handler来处理这些,可以在一个线程的run方法中调用handler对象的 postMessage或sendMessage方法来实现,Android程序内部维护着一个消息队列,会轮训处理这些。 Looper又是什么呢? ,其实Android中每一个Thread都跟着一个Looper,Looper可以帮助Thread维护一个消息队列,但是Looper和Handler没有什么关系,我们从开源的代码可以看到Android还提供了一个Thread继承类HanderThread可以帮助我们处理,在HandlerThread对象中可以通过getLooper方法获取一个Looper对象控制句柄,我们可以将其这个Looper对象映射到一个Handler中去来实现一个线程同步机制,Looper对象的执行需要初始化Looper.prepare方法就能解决上面提到的问题。Message 在Android是什么呢? 对于Android中Handler可以传递一些内容,通过Bundle对象可以封装String、Integer以及Blob二进制对象,我们通过在线程中使用Handler对象的 sendEmptyMessage或sendMessage方法来传递一个Bundle对象到Handler处理器。对于Handler类提供了重写方法handleMessage(Message msg) 来判断,通过msg.what来区分每条信息。将Bundle解包来实现Handler类更新UI线程中的内容实现控件的刷新操作。相关的Handler对象有关消息发送sendXXXX相关方法如下,同时还有postXXXX相关方法,一个为发送后直接返回,一个为处理后才返回
- 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() 终极解决方法
- 从现在开始,成为一个saper
- 关于UIView的背景图片问题
- 车辆在事故扣押期间的停车费和鉴定费
- 日经社説 20150525 TPP実現へ重大な責任を負う米議会
- 如何计算算法的时间复杂度
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- Java 理论与实践: 流行的原子
- 黑马程序员———反射
- 社説 20150525 空き家対策法 危険になる前に手を打とう
- easyui layout+tab+tree实现网站基本布局
- js 操作cookie
- iOS中登录功能的体验探究
- 社説 20150525 伊方原発「合格」 再稼働へ着実に準備を進めよ
- 什么时候使用Looper.prepare()