Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()
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()的更多相关文章
- Android Exception 13(Can't create handler inside thread that has not called Looper.prepare())
10-12 17:02:55.500: E/AndroidRuntime(28343): FATAL EXCEPTION: Timer-2 10-12 17:02:55.500: E/AndroidR ...
- Android 错误提示: Can't create handler inside thread that has not called Looper.prepare()
Can't create handler inside thread that has not called Looper.prepare() 将 Handler handler = new Hand ...
- Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误
原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...
- Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()
MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...
- Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()
问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...
- 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- Can’t create handler inside thread that has not called Looper.prepare()
1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...
- Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
章出自:luchg技术交流 http://www.luchg.com 版权所有.本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源,谢谢. Android-java.lang.Runti ...
- 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
随机推荐
- 原生js之canvas时钟组件
canvas一直是前端开发中不可或缺的一种用来绘制图形的标签元素,比如压缩上传的图片.比如刮刮卡.比如制作海报.图表插件等,很多人在面试的过程中也会被问到有没有接触过canvas图形绘制. 定义 ca ...
- 位运算n & (n-1)的妙用
本文转自:http://blog.csdn.net/zheng0518/article/details/8882394 按位与的知识 n&(n-1)作用:将n的二进制表示中的最低位为1的改为0 ...
- YARN整理
YARN整理 1.YARN的介绍 是一个资源管理.任务调度的框架,主要包含三大模块: ResourceManager(RM):负责所有资源的监控.分配和管理 ApplicationMaster(AM) ...
- 浅谈Log4net在项目中如何记录日志
一 引入背景 在软件开发周期中,无论是开发中,或是测试中,或是上线后,选择合适的工具监控程序的运行状态至关重要,只有如此,才能更好地排查程序问题和检测程序性能问题等.本篇文章主要与大家分享,如何 ...
- 在web应用中使用Log4j 2
Using Log4j 2 inWeb Applications (在web应用中使用Log4j 2) 来源:http://logging.apache.org/log4j/2.x/manual/we ...
- mybatis映射器配置细则
前面三篇博客我们已经多次涉及到映射器的使用了,增删查基本上都用过一遍了,但是之前我们只是介绍了基本用法,实际上mybatis中映射器可以配置的地方还是非常多,今天我们就先来看看映射器还有哪些需要配置的 ...
- [LaTeX] box algorithm adjustbox subfigures, textwidth
algorithm \begin{figure}[!t] \vspace*{\dimexpr\fboxsep+\fboxrule} \fbox{% \parbox{\dimexpr\linewidth ...
- mysql 常见语句
事务 默认是开启了autocommit,可以通过show variables like 'autocommit';查看. 如果临时需要取消autocommit,可以通过START TRANSACTIO ...
- 安卓高级5 传感器和震动 模仿微信摇一摇Ui效果
效果图: 所用的Ui就三张图: 案例代码: 结构 MainActivity.java package com.example.myapp; import android.content.Intent; ...
- MS Office2016留下的坑
背景 问题源自论坛用户反馈,他用管家有几年了,之前使用IE都很正常,没有任何问题,但是最近突然发现,启动IE时,就会出现系统错误提示:无法启动此程序,因为计算机中丢失 api-ms-win-core- ...