Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()
来源:互联网 发布:哪种网络编程语言 编辑:程序博客网 时间:2024/05/22 08:30
Error:Can't create handler inside thread that has not called Looper.prepare()
原代码:
//利用Handler消息传递机制
final Handler myHandler = new Handler(MainActivity.this.getMainLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123){
//置于此处有问题 (初步估计是线程间问题)
Toast toast = Toast.makeText(MainActivity.this, "祝您旅途愉快!", Toast.LENGTH_LONG);
toast.show();
}
}
};
//发送空消息
myHandler.sendEmptyMessage(0x123);
搜索后发现,原因是此Handler没有Looper。到哪儿去找Looper呢?自己建?
在代码前加入Looper.prepare();,心想这回可以了吧?
没想到依然报错,错误显示,一个主进程只能有一个Looper,要死了。郁闷中...
突然我想到主进程中肯定有Looper,Context.getMainLooper(),再看Handler的实例化时是可以指定Looper的,太爽了,最后代码如下
//利用Handler消息传递机制
final Handler myHandler = new Handler(MainActivity.this.getMainLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123){
//置于此处有问题 (初步估计是线程间问题)
Toast toast = Toast.makeText(MainActivity.this, "祝您旅途愉快!", Toast.LENGTH_LONG);
toast.show();
}
}
};
//发送空消息
myHandler.sendEmptyMessage(0x123);
- Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()
- 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 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()
- 为什么开发程序首选C#语言
- c++中const变量真的不可以修改吗?
- java实现二叉排序树
- POJ 2499 Binary Tree
- 第八周阅读四
- Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()
- 【原创】IP摄像头技术纵览(一)---linux 内核编译,USB摄像头设备识别
- P123.36(请编写一个函数printdate(int year,int month,int day ),将输入的三个数字转化成英语纪年输出。)
- 1.数据存储与类型转换
- 工作日志 4.25
- Spark K-Means
- C++内存结构总结
- Frobenius Norm
- SDKD TeamContest F 题解