Can't create handler inside thread that has not called Looper.prepare()
来源:互联网 发布:网络打印机添加xp 编辑:程序博客网 时间:2024/06/06 12:36
今天在子线程的handler里dismiss一个对话框,然后就报“Can't create handler inside thread that has not called Looper.prepare()”这个错。
他说“在没有调用Looper.prepare()之前,handler无法创建程序”
于是我就奇怪了 Looper是什么鬼。
参考以下博客:
http://blog.csdn.net/lmj623565791/article/details/38377229
http://blog.sina.com.cn/s/blog_78bb6d040101axl4.html
http://blog.csdn.net/hitlion2008/article/details/7561190
这才明白:
(1)一般在非 UI 的线程中不允许调弹出框,对话框之类的东东。
(2)Looper类别用来为一个线程开启一个消息循环。默认情况下Android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环),默认情况下Handler会与其被定义时所在线程的Looper绑定
(3)如果非主线程需要循环处理其他线程发来的消息,或者进行长期的复杂交互,则需要Looper建立消息队列。
解决方法:
Loope.prepare(),初始化线程消息队列。
Looper.loop(),线程的消息队列开始运行,接收消息。
想要退出消息循环时,调用Looper.quit()注意,这个方法是要在对象(先调用Looper.myLooper()获取Looper对象)上面调用
简单的来说:用Looper.prepare()和Looper.loop()将你的代码包起来。
(4)强烈推荐我参考的第一条博文,很好地解释异步消息处理机制(Looper,Message,Handler)
- 解决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()解决办法
- 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()解决办法
- POJ2778 DNA Sequence(AC自动机+矩阵快速幂)
- Java NIO:浅析I/O模型
- Android自带的toolbox分析及扩展
- 【poj 1502】 MPI Maelstrom 【East Central North America 1996】
- Travel HDU
- Can't create handler inside thread that has not called Looper.prepare()
- 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers
- 菜鸟学框架——Mybatis核心组件
- 4:如何实现对两个整数变量的值进行互换。
- Linux/Centos:如何安装GCC(C和C++编译器)
- pymysql+Flask-SQLAlchemy连接mysql
- 我眼中的数据仓库
- Android_RecycleView基本详解
- Linux笔记(二)常用命令