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()的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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报 ...

  4. 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 ...

  5. Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

    问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...

  6. 在子线程中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 ...

  7. Can’t create handler inside thread that has not called Looper.prepare()

    1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...

  8. 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 ...

  9. 【转】在子线程中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 ...

随机推荐

  1. php判断浏览器是不是IE

    1.$_SERVER['HTTP_USER_AGENT']和strpos 2.打印结果 谷歌: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl ...

  2. Docker学习系列(一)Docker简介

    简介 Docker是一个在全球范围领先的软件容器平台.开发人员可以使用容器来在协作过程中遇到的解决"不同环境配置"的问题.(例如,在传统的开发环境下,开发人员编写代码,然后交由测试 ...

  3. Oracle中备份用户对象的两种方法

    方法1: 执行步骤: exp userid=用户名/密码@数据库名 file=c:\emp.dmp 使用当前用户导出 exp userid=sys/sys@数据库名 file=c:\emp.dmp o ...

  4. Python List insert()方法详解

    1.功能insert()函数用于将指定对象插入列表的指定位置. 2.语法list.insert(index, obj) 3.参数index: 对象obj需要插入的索引位置.obj: 插入列表中的对象. ...

  5. PHP 安装

    PHP 安装 您需要做什么? 为了开始使用 PHP,您可以: 找一个支持 PHP 和 MySQL 的 Web 主机 在您自己的 PC 机上安装 Web 服务器,然后安装 PHP 和 MySQL 使用支 ...

  6. 20160219.CCPP体系详解(0029天)

    程序片段(01):ReplaceAll.c 内容概要:ReplaceAll #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #incl ...

  7. hadoop入门级总结二:Map/Reduce

    在上一篇博客:hadoop入门级总结一:HDFS中,简单的介绍了hadoop分布式文件系统HDFS的整体框架及文件写入读出机制.接下来,简要的总结一下hadoop的另外一大关键技术之一分布式计算框架: ...

  8. Mac状态栏wifi图标一直闪烁重复连接但是网络正常的解决办法

    本猫的系统是EI(10.11.6),不知从哪个版本开始(至少是升级到EI之后),状态栏上的wifi图标一直闪烁,这应该是表示正在连接网络.但是网络是正常的! 虽说闪烁的wifi图标不影响使用,但是有强 ...

  9. Python实现数据库一键导出为Excel表格

    依赖 Python2711 xlwt MySQLdb 数据库相关 连接 获取字段信息 获取数据 Excel基础 workbook sheet 案例 封装 封装之后 测试结果 总结 数据库数据导出为ex ...

  10. 初识gd库

    必备基础 开启GD拓展 列表使用 获取图片信息代码 图片详细信息 特效函数 示例 运行结果 分析 获取图片基本信息 获取图片宽度 获取图片高度 获取图片后缀名 获取图片mime类型 操作图片 添加文字 ...