[Android]Can't create handler inside thread that has not called Looper.prepare()
来源:互联网 发布:js中给span赋值 编辑:程序博客网 时间:2024/05/16 07:10
都是因为在新开的线程中更新UI才出错,子线程中更新主线程UI需要用到Handler.
今天有出现如下错误,代码如下:
send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "主线程中UI", Toast.LENGTH_SHORT).show(); } }); } });
Toast.makeText(getApplicationContext(), "主线程UI", Toast.LENGTH_SHORT).show();都是这句话惹的祸。
把上面这行代码也通过处理放进handler之后,错误就没有了。
Handler是这么定义的:
主要接受子线程发送的数据, 并用此数据配合主线程更新UI.
Handler的主要作用:主要用于异步消息的处理
Handler的运行过程:
当(子线程)发出一个消息之后,首先进入一个(主线程的)消息队列,发送消息的函数即刻返回,而在主线程中的Handler逐个的在消息队列中将消息取出,然后对消息进行处理。这样就实现了跨线程的UI更新(实际上还是在主线程中完成的)。
这种机制通常用来处理相对耗时比较长的操作,如访问网络比较耗时的操作,读取文大文件,比较耗时的操作处理等。
0 0
- Unity-Android: 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()
- android之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()解决方案
- [Android]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()错误!
- Android中的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()
- Android : 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()
- Android : 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()
- Android java.lang.RuntimeException: 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()
- Android开发报错 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()错误
- Android开发报错 Can't create handler inside thread that has not called Looper.prepare()
- Android未调用Looper.prepare()-Can't create handler inside thread that has not called Looper.prepare()
- POJ 2664 Prerequisites?(简单题)
- Android ----------- 事件分发 机制 研究
- 摄像头远程web控制
- Linux正则学习
- CString 字符串的连接
- [Android]Can't create handler inside thread that has not called Looper.prepare()
- 第二章线性表总结(包括知识总结导图)
- 第四章 4.8节练习
- 人嘛,就那么回事!
- HDU3351 Seinfeld 【贪心】
- hdu 4996 1~n排列LIS值为k个数
- C語言 rand函数 进阶探讨与实现
- 2014 第五届蓝桥杯软件本科A组预赛题解(填空及代码填空)
- collection集合的简单使用