java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

原因1:

子线程直接更新ui

8月9日在service中创建子线程时再次遇到

java.lang.RuntimeException: Can't create handler inside thread Thread that has not called Looper.prepare()

这个问题。代码如下

new Thread(new Runnable() {
                @Override
                public void run() {
                    timer = new Timer(mTotalTime, TimeSetted.SECOND_TO_MILL);
                    timer.start();
                    Log.d(TAG,"Countdown start");
                }
            }).start();
 修改为

new Thread(new Runnable() {
                @Override
                public void run() {
                    Looper.perpare();//增加部分
                    timer = new Timer(mTotalTime, TimeSetted.SECOND_TO_MILL);
                    timer.start();
                    Log.d(TAG,"Countdown start");
                    Looper.loop();//增加部分
                }
            }).start();
后,应用正常运行。即在具体逻辑的前后加入Looper.perpare()和Looper.loop()方法。

查了一下报错原因:非主线程中没有开启Looper,而Handler对象必须绑定Looper对象需要调用Looper.prepare()来给线程创建一个消息循环,调用Looper.loop()来使消息循环起作用。

问题来了,

在这个子线程和之前的doInBackground中,都没有new一个handler,为什么会出现这个错误?

查看了一下代码中使用的CountdownTimer的源码,start(),pause(),cancel()方法都是用message和handler实现的,在子线程中使用CountdownTimer时,由于没有默认绑定Looper,因此会报错。

结论:

1.Can't create handler inside thread Thread that has not called Looper.prepare()通常是在子线程中使用没有绑定Looper的handler时出现,只需在handler语句的前后加Looper.prepare()和Looper.loop()方法即可。

2.创建子线程和使用AsyncTask的doInBackground时,若没有new一个handler对象,通常不会出现这个错误。本人是由于在调用其他方法时用了handler导致报错。

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()相关推荐

  1. Android --- java.lang.RuntimeException: Can‘t create handler inside thread that has not called Loop

    报错信息如下: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr ...

  2. java.lang.RuntimeException: Can't create handler inside thread that has not

    在子线程中 调用了这句: Toast.makeText(UiUtils.getContext(), "正在下载"+name, Toast.LENGTH_SHORT).show(); ...

  3. Can't create handler inside thread that has not called Looper.prepare() 解决办法

    在开发的过程当中,遇到了想在一个线程中弹出一个提示窗 new AlertDialog.Builder(            context),但是就出现了一个问题. java.lang.Runtim ...

  4. Can't create handler inside thread that has not called Looper.prepare()

    问题: Can't create handler inside thread that has not called Looper.prepare() 1,在报错的方法前加Looper.prepare ...

  5. MQ java.lang.OutOfMemoryError: unable to create new native thread

    文章目录 1. 问题现象 2. 分析定位 3. 解决方案 4. 结果验证 5. 总结 1. 问题现象 java.lang.OutOfMemoryError: unable to create new ...

  6. 解决java.lang.OutOfMemoryError: unable to create new native thread问题

    解决java.lang.OutOfMemoryError: unable to create new native thread问题 参考文章: (1)解决java.lang.OutOfMemoryE ...

  7. spark大批量读取Hbase时出现java.lang.OutOfMemoryError: unable to create new native thread

    这个问题我去网上搜索了一下,发现了很多的解决方案都是增加的nproc数量,即用户最大线程数的数量,但我修改了并没有解决问题,最终是通过修改hadoop集群的最大线程数解决问题的. 并且网络上的回答多数 ...

  8. Can't create handler inside thread Thread that has not called Looper.prepare()

    今天在子线程里面写了一个Toast 运行程序直接崩溃了 看了下log 如下 最后忽然想到了子线程里面不能直接使用Toast 然后 就添加了 Looper.prepare(); 和 Looper.loo ...

  9. Caused by: java.lang.RuntimeException: Font asset not found fonts/SYFZLTKHJW.TTF

    /*** 通过反射方法设置app全局字体*/public void setTypeface() {typeFace = Typeface.createFromAsset(getAssets(), &q ...

最新文章

  1. linux下用iptables做本机端口转发方法(转载)
  2. WAIC2020开幕在即,第四范式亮点抢先看
  3. html 横屏内容显示不全_为什么我的文本显示不全?
  4. 基于Matlab的循环码实验报告,基于MATLAB的循环码实验报告
  5. linux gcc出错,编译arm-linux-gcc出错
  6. 基于java SSM springboot+redis网上水果超市商城设计和实现以及文档
  7. spring mvc 总体概况
  8. Mac OS X安装 ffmpeg
  9. 三菱plc232数据线驱动下载_失易得安卓恢复v5.3.5.0-失易得安卓恢复PC版下载
  10. C语言用冒泡法对数组元素降序,冒泡法排序c语言
  11. android计算器实现sin功能,android studio实现简单的计算器功能
  12. java 使用poi导出excel柱状图
  13. go mysql 中间件_GitHub - wushilong/go-sharding: Mysql 分库分表中间件
  14. AMD CPU 超频
  15. 国产银河数学式电子计算机是属于,《世界上公认的第一台电子计算机.doc
  16. 广播风暴和环路是什么
  17. Web报表系统葡萄城报表:报表设计
  18. TUP首期主题论坛报道:中小型开发商移动开发的生存之道
  19. 河北省对口升学计算机专业学校,河北省对口升学计算机专业试题详解
  20. 前端开发之走进Vue.js(入门者看过来)

热门文章

  1. 提权函数之RtlAdjustPrivilege()
  2. java设计模式---三种工厂模式
  3. 构造函数的初始化列表
  4. 使用QEMU创建虚拟机
  5. 谈谈Linux打补丁的原理以及如何判别打补丁的错误 --- 从补丁学内核
  6. android系统动态切换输入法,一种动态切换Android系统输入法的弹出模式的方法与流程...
  7. php如何防止消息被篡改,php如何用libevent处理rabbitmq发来的消息,防止消息丢失或者人为的中断导致消息没有被处理完整...
  8. 499php,一笔画仙路 [499]第499章 神器的威力 最新章节无弹窗全文免费阅读 乐文阅读官网...
  9. Linux空间过满无法登录,linux下磁盘空间不足导致oracle无法登录的解决方案
  10. hive 写入mysql 覆盖_替换Hive的元数据库derby