Android 异常 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper
来源:互联网 发布:会计中级网络培训推荐 编辑:程序博客网 时间:2024/06/07 03:34
调用 handler 使用报错java.lang.RuntimeException: Can't create handler inside thread that has not called Looper
在你的类中加
和在你的ManiFestFile中加:
介绍:
Toast和Looper。Handler消息循环机制。
(1) Looper类别用来为一个线程开启一个消息循环。默认情况下Android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环)
Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。
(2) 通常是通过Handler对象来与Looper交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。
默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,在主线程中定义,其是与主线程的Looper绑定。
mainHandler = new Handler() 等价于new Handler(Looper.myLooper()).
Looper.myLooper():Return the Looper object associated with the current thread 获取当前进程的looper对象。
还有一个类似的 Looper.getMainLooper() 用于获取主线程的Looper对象。
(3) 在非主线程中直接new Handler() 会报如下的错误:
E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due to uncaught exception
E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。
(4) Looper.loop(); 让Looper开始工作,从消息队列里取消息,处理消息。
注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。
(5) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。
- 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 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()
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
- 详谈 Jquery Ajax 异步处理Json数据
- 题目1518:反转链表
- 模式匹配算法-KMP算法
- EABI ABI 嵌入式应用二进制接口
- myeclipse安装 配置Maven
- Android 异常 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper
- UVa 103 Stacking Boxes (DP&DAG)
- 黑马程序员Java基础__GUI
- Excel数据,dataGridview, openFileDialog
- 10款免费且开源的项目管理工具
- Oracle 等待事件类型wait_class说明
- 终于开了一个CSDN博客
- 使用Timer、AlarmManager实现定时服务+Notification进行模拟推送的实现
- 如果说有些难题注定要解决的要淡定面对现实