Android java.lang.RuntimeException: Can't create handler inside thread that has not called
来源:互联网 发布:彝族音乐 知乎 编辑:程序博客网 时间:2024/05/24 06:43
在Android 5.1机顶盒开发中遇到问题:
java.lang.RuntimeException: Can't create handler inside thread that has not called
调用android.net.ethernet.EthernetManager出现问题,打印日志如下
升级代码中 FHUbootReceiver 中126行
EthManagerUtils 中接口方法如下
./vendor/amlogic/frameworks/core/java/android/provider/fhagent/EthernetManagerInterface.java 中127行
在framework层./frameworks/base/core/java/android/net/ethernet/EthernetManager.java中67行
在android中提供了一种异步回调机制Handler,使用它,我们可以在完成一个很长时间的任务后做出相应的通知
handler基本使用:
在主线程中,使用handler很简单,new一个Handler对象实现其handleMessage方法,在handleMessage中提供收到消息后相应的处理方法即可,这里不对handler使用进行详细说明,查阅资料,主要探究一下handler的内部机制。
比如使用myThreadHandler.sendEmptyMessage(0);发送一个message对象,那么Handler是如何接收该message对象并处理的呢?我先画一个数据结构图:
调用sendEmptyMessage后,会把 Message对象放入一个MessageQueue队列,该队列属于某个Looper对象,每个Looper对象通过 ThreadLocal.set(new Looper())跟一个Thread绑定了,Looper对象所属的线程在Looper.Loop方法中循环执行从MessageQueue队列读取 Message对象,并把Message对象交由Handler处理,调用Handler的dispatchMessage方法。
主线程它已经建立了Looper,这是从android 源码中得知,而新建线程则没有。
原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。
解决方法:
。。。。。。
至此,解决了问题
- 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.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.prepare()
- Android java.lang.RuntimeException: Can't create handler inside thread that has not called
- android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- 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()
- 关于for循环的嵌套
- JAVA-线程系列教程<3>-线程的几种状态
- 第九条: equals和hashCode
- 论文笔记:Network in Network
- java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
- Android java.lang.RuntimeException: Can't create handler inside thread that has not called
- Hive高级进阶与优化
- Qt设置启动画面和广告画面
- C语言常见命名规范
- Excel解析
- JAVA从菜鸟【入门】到新手【实习】一一ASP技术的若干思考
- HashMap常见面试题(完善中)
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-05-模型
- oracle v_$session和v$session