Can't create handler inside thread that has not called Looper.prepare()问题解决

【报错提示】java.lang.RuntimeException: Can‘t create handler inside thread 遇到一个报错: java.lang.RuntimeException: Can't create handler inside thread Thread[OkHttp https://a.fxltsbl.com/...] that has not called Looper.prepare() 1. 这个报错提示是在一个没有调用 Looper.prepare() 的线程中尝试创建一个 Handler 对象。 阅读详情

写在前面:

我们都知道,UI的显示要在UI线程中执行,而不能在子线程中,也不能在异步线程中执行。可是,有时候,不知不觉就会违反这一规则,下面,就是一例,亲身经历。


1. 首先,看一下错误信息:

 W/dalvikvm(25729): threadid=14: thread exiting with uncaught exception (group=0x413e7360)
: E/AndroidRuntime(25729): FATAL EXCEPTION: AsyncTask #2
: E/AndroidRuntime(25729): java.lang.RuntimeException: An error occured while executing doInBackground()
 E/AndroidRuntime(25729): at android.os.AsyncTask$3.done(AsyncTask.java:299)
 E/AndroidRuntime(25729): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
: E/AndroidRuntime(25729): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
: E/AndroidRuntime(25729): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
: E/AndroidRuntime(25729): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
: E/AndroidRuntime(25729): at java.lang.Thread.run(Thread.java:856)
: E/AndroidRuntime(25729): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
: E/AndroidRuntime(25729): at android.os.Handler.<init>(Handler.java:121)
: E/AndroidRuntime(25729): at android.widget.Toast$TN.<init>(Toast.java:335)
: E/AndroidRuntime(25729): at android.widget.Toast.<init>(Toast.java:101)
: E/AndroidRuntime(25729): at android.widget.Toast.makeText(Toast.java:248)
......
: E/AndroidRuntime(25729): at android.os.AsyncTask$2.call(AsyncTask.java:287)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
: E/AndroidRuntime(25729): ... 5 more


2. 分析:


很明显,是由于AsyncTask和handler运用的不正确。大多是由于在异步线程中调用了UI相关函数。


3. 具体到本次错误的原因,是因为在异步线程中调用了Toast来。代码片段如下:


private MyAsyncTask mMyAsyncTask;


private MyAsyncTask extends AsyncTask.... {
...
@Override
protected void onPostExecute(String result) {
if (result.equals("0")) {

mMyTestListener.onEvent("OK");   //通过接口的方式将异步线程的执行结果传到调用层。
}else {
mMyTestListener.onEvent(null);
}


}


private MyHandler mMyHandler;
private class MyHandler extends Handler {

public void start() {
sendEmptyMessage(0);
}

@Override
public void handleMessage(Message msg) {
Log.i(TAG, "handleMessage msg ===" + msg);
Object obj = msg.getData();
if (msg.what == 0) {
doSomething();
}else {

Toast.makeText(mContext, ""+ obj, Toast.LENGTH_LONG).show();  //修改后的
}

}
};






/**
* 用于更新ui
*/
private MyTestListener mMyTestListener = new MyTestListener();


class MyTestListener implements MyBaseListener {



@Override
public void onEvent(String res) {

Message msg = new Message();
msg.what = 1;
msg.obj = res;

if (res!=null) {
   doSomething();
}else {
   Toast.makeText(mContext, ""+ obj, Toast.LENGTH_LONG).show();  //修改前的,error
}

}

}

4. 总结:

   1.  将Toast放在handler中显示。

   2. 通过interface在异步线程中调用它的函数,这个interface的实现一定不能进行UI的操作。


复现CVE-2014-085(Apache ZooKeeper 未授权访问漏洞) 默认安装配置完的zookeeper允许未授权访问,管理员未配置访问控制列表(ACL)。导致攻击者可以在默认开放的2181端口下通过执行envi命令获得大量敏感信息(系统名称、java环境)导致任意用户可以在网络不受限的情况下进行未授权访问读取数据。 阅读详情

相关推荐

Zemax热分析实战:5分钟搞定双胶合透镜温度漂移优化(附操作数详解)

本文详细解析了使用Zemax进行光学系统热分析的全流程,以双胶合透镜为例,重点阐述了如何通过多重结构编辑器模拟温度变化,并利用关键操作数优化系统以抵抗温度漂移。文章提供了从基础设置到高级优化的实战技巧,帮助工程师快速掌握无热化设计方法,确保系统在宽温范围内保持稳定成像性能。

n4o5p6q7r的博客 396

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

今天记录一下写安卓的时候遇到的些问题吧,都是比较常见实用的1、错误:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()2、简单的okhttp的Get请求。Json转数组总结: 博主并不是专业的安卓开发,所以遇到的一些问题也可能比较低级,解决方案也并非...

Mr_Van的博客 3578

代码实现ROS2bag录制数据和回放数据(C++)

ROS2的ROS2bag功能可以将指定Topic中的数据以.db3的格式(不同于ROS)保存到本地文件中。读取文件数据可以实现数据回放。

m0_57124501的博客 3256

Toast和Looper。Handler消息循环机制。

(1) Looper类别用来为一个线程开启一个消息循环。默认情况下Android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环) Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。 (2) 通常是通过Handler对象来与Looper交互的。Han...

JoyChiang的专栏 475

LK32T102 单片机 操作GPIO实现流水灯(1)

前言: LK32T10232(32位)单片机是一款专为电机控制器和数字电源开发的高性能 MCU。采Cortex-M0 内核,最高工作频率可达 72Mhz,ADC 采样速率 达 1Msps。同时具备协处理器单元可满足各种乘法、除法、移位以及各种 复杂函数运算。 LK32T102(32位)支持单电源供电,且内嵌高精度高速及低速振荡器,以及具备 多种低功耗工作模式。同时,集成了多路增强型 PWM,多通道模拟比较 器和高速运算放大器,可满足多种电机及功率控制应用,并简化系统成本。...

qq_58676187的博客 3189

CAD二次开发(C#版):从零搭建开发环境与核心DLL引用指南

本文详细介绍了使用C#进行CAD二次开发的环境搭建与核心DLL引用指南,涵盖Visual Studio配置、AutoCAD插件项目创建、DLL引用技巧及调试方法。通过实战示例和常见问题排查,帮助开发者快速掌握CAD二次开发的核心技术,提升开发效率。

weixin_28837817的博客 727

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

[java] view plaincopy private Handler mHandler = new Handler(){                          public void handleMessage(Message msg) {                  switch (msg.what) {

LVXIANGAN的专栏 4641

自学PCB思路 | PCB设计零基础入门-小白适用(附详细入门经验分享)

本人大一在实践课上就听说PCB,被虎的一楞一楞的,不过现在回看,发现其实并没有相信的那么难!想着把自己这一路摸索的学习路线分享一下,当时我是一边摸索一边上手,一把鼻涕一把泪的过来的。如果你想搞硬件或者需要搞硬件,下面是我的入门学习过程,希望可以帮到你。目录前言选择一款设计软件好的入门课程掌握抄板元件采购焊板子烙铁头热风枪自己半设计明确设计需求全设计最后我并非什么大佬,也没法自己去教大家如何画板。

自动化小秋葵的博客 1万+

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

 8月9日在service中创建子线程时再次遇到 java.lang.RuntimeException: Can't create handler inside thread Thread that has not called Looper.prepare() 这个问题。代码如下 new Thread(new Runnable() { @Override ...

雷霆管理层的博客 8万+

rfid标签技术原理

奇怪的是,RFID标签技术原理的深入剖析不仅揭示了其背后的奥秘,也让我们更加全面地认识到这一技术在数字化转型中的重要性和潜力。随着技术的不断进步和应用场景的不断拓展,RFID标签将在更多领域发挥重要作用,为物联网、智能制造等领域的发展注入新的活力。这意味着在一个读写器的覆盖范围内,可以同时读取多个RFID标签的数据,大大提高了数据收集的速度和效率。:RFID标签的“大脑”,负责存储和处理数据。芯片内部集成了存储器、控制逻辑和射频接口等电路,能够接收来自读写器的射频信号,执行相应的数据处理和通信任务。

x3088的博客 2157

【在 WSL2 GUI 中使用 pulseaudio 播放声音】

最近在搞一个Linux项目,由于WSL2中需要用到声音外放,无奈只能潜心研究。后来就使用了一种折中的方式替代,那就是pulseaudio。目前来讲WSL2对硬件外设的支持还是不太友好,关于USB的支持我之前在其他文章中已经论述过了,这里不讨论。1、Windows11别忘记打开声音2、可能有一定延迟,因为是通过网络传输的。3、效果的话只能见仁见智了,如果不能满足要求就安装一个实实在在的Ubuntu吧!

jiexijihe945的博客 4530

Android问题集锦之五:Can't create handler inside thread that has not called Looper.prepare()错误的解决

此问题是和UI线程同步引起的。第一个办法是根据提示,添加 Looper.prepare();  Thread action=newThread() { publicvoidrun() { Looper.prepare();  todo();  Looper.loop(); } }

懒人的技术笔记 3665

核磁共振常用的文件格式介绍:NIfTI volume 格式 (*.nii), GIFTI 格式 (*.gii), CIFTI 格式 (*.nii)

NIfTI volume 格式 (*.nii)GIFTI 格式 (*.gii)CIFTI 格式 (*.nii)Scene file文件 (*.scene)规格文件 (*.spec)解剖 (*.annot)一个场景文件包含一个或多个场景;每个场景实际上是保存时 wb_view 内部状态的快照。这包括有关加载哪些文件、如何显示、如何配置选项卡的信息(选择了哪些表面、在每个层中选择了哪些文件、打开了哪些层、哪些视图旋转、调色板设置、平铺选项卡等),以及在它们上面添加了哪些注释(文本、箭头、圆圈等)。

从事脑科学核磁共振方法学研究,在Nature communications等权威期刊发表研究论文,熟练掌握磁共振处理方法和统计学方法,欢迎大家和我交流。 3042

Can't create handler inside thread that has not ca

在Android开发中,如果在一个Thread中启动了一个ProgressDialog用来提示正在连接(也就是防假死),然后在Handler中dismiss()这个ProgressDialog,程序就直接挂掉,异常信息是:Can't create handler inside thread t...

chengjia0847的博客 386

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

Looper.prepare(); // Can't create handler inside thread that has not called Looper.prepare()。 Toast.makeText(context,"登录超时",Toast.LENGTH_SHORT).show(); ...

Choi晨的博客 924

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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()这个问题,碰到这个问题最常用的办法,就是创建handler 然后在handler的handlerMessage()方法中进行界面操作,可是 也有些特殊的情况比如我今天遇到的:我有两个类,A,B,A

keke921231的专栏 921

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Can't create handler inside thread that has not called Looper.prepare() 终极解决方法

E/AndroidRuntime(26639): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 在报错的方法前加Looper.prepare();  方法末尾加Looper.loop(); 很简单

抢财猫股票课堂 1万+

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

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 这时今天遇到的第二个错误。 原因是非主线程中没有创建Looper对象,需要先调用Looper.prepare()启用Looper。 解决办法是在线程的开始跟结束间调用Looper.prepa...

hei5207的博客 468

Okhttp3使用问题处理

一、导入设置 implementation 'com.squareup.okhttp3:okhttp:3.10.0' 二、使用 private void fillData() { String url =getString(R.string.serverurl); OkHttpClient okHttpClient = new OkHttpClient(); ...

圣人男的博客 1498

子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误...

直接上原文,地址:http://hi.baidu.com/_guangren_/item/8e61f31cecad9a701009b5b7 今天用子线程调Toast报了一个Can'tcreatehandlerinsidethreadthathasnotcalledLooper.prepare()错误。在网上找了找答案,发现两篇文章: 【1】http://czhjchin...

modabobo 240

线程中调用Toast闪退解决方案

会报:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 错误,一直以为是我的Socket的问题,接模拟器看Logcat才发现是Toast的问题,查了资料: 在调用Toast(或者AlertDialog)的地方的前面加上Looper.prepare(

need_just_word的博客 1906
上一篇: Android中的服务(service)详解(四)--系统服务
下一篇: Android中设置、打开GPS的两种方法,包括反射的应用
liranke
博客等级 码龄20年 4939粉丝 594原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值