Android—子线程更新UI问题( java.lang.RuntimeException: Can't create handler inside thread that has not cal)
来源:互联网 发布:mvc权限管理系统源码 编辑:程序博客网 时间:2024/06/10 09:14
之前在子线程中显示一个UI操作,发生错误
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()代码为:延时3秒后发出一个Toast信息
new Thread(){ public void run(){ try { Thread.sleep(2999); Toast.makeText(MainActivity.this, "请重新连接",Toast.LENGTH_LONG).show(); } catch (InterruptedException e) { } } }.start();
这里明显在子线程进行UI更新,所以发生错误
解决:在子线程中发送一个message到Handle,让Handle更新线程
代码:
线程:
new Thread(){ public void run(){ try { Thread.sleep(2999); mHandler.sendEmptyMessage(0); } catch (InterruptedException e) { } } }.start();
Handle写在onCreate()之外
Handler mHandler = new Handler() { public void handleMessage(Message msg) { Toast.makeText(MainActivity.this, "请重新连接",Toast.LENGTH_LONG).show(); super.handleMessage(msg); } };
0 0
- Android—子线程更新UI问题( java.lang.RuntimeException: Can't create handler inside thread that has not cal)
- java.lang.RuntimeException: Can't create handler inside thread that has not cal
- 线程 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepa
- 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()
- 互联网公司常见面试算法题
- 变频器维修中电阻的测量方法
- Xpath解析之:几个常纠结的节点选取语法总结
- 数据结构Java实现05----栈:顺序栈和链式堆栈
- virtio_blk 通过写virtqueue 出发中断通知qemu后端
- Android—子线程更新UI问题( java.lang.RuntimeException: Can't create handler inside thread that has not cal)
- fiddler抓包
- 最大流(模板)
- mysql注释解释详见
- Linux MySQL修改密码时 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- SSH三大框架的工作原理及流程
- 配置可以上网的Host-only网络
- java开发规范
- 用NLTK进行自然语言处理的项目