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

LLM - windows下的Dify离线部署:从镜像打包到无网环境部署(亲测,包含插件部署) 阅读详情

为什么在子线程中创建handler时会报以下错误

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

 

因为Android系统默认情况下:非主线程中没有开启Looper,而Handler对象必须绑定Looper对象。

第二个子线程中注意要加上 Looper.prepare(); Looper.loop();

ex:

private Thread notifyingThread;
private Thread secondThread;
private TextView tv_show;


//创建第一个子线程
notifyingThread = new Thread(null, mTask, "子线程1");
notifyingThread.start();


private Runnable mTask = new Runnable() {
    public void run() {
        //第二个子线程
        secondThread = new Thread(null,secondRunable,"子线程2");
        secondThread.start();
    }
};


private Runnable secondRunable = new Runnable() {
    @Override
    public void run() {
        Looper.prepare();
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch(msg.what){
                    case 1:
                        mHandler.sendEmptyMessage(0);
                        break;
                }
            }
        };
        handler.sendEmptyMessageDelayed(1,2000);
        Looper.loop();
    }
};

//更新视图的handler
Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0:
                tv_show.setText("dsfasdf");
                break;
            default:
                break;
        }
    }

};
基于学术研究于NASA官网GPM卫星数据下载详细教程 阅读详情

相关推荐

如何在OBS中添加实时时间戳:Lua脚本配置教程(附效果展示)

本文详细介绍了如何在OBS中通过Lua脚本配置实时时间戳功能,包括环境准备、脚本获取、安装配置及高级定制。适用于直播和视频录制场景,提供精准回溯和风格自定义,支持多平台适配和性能优化,是专业制作的必备技巧。

day7的博客 1044

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描述解决方法123完事 描述 这个错误发生在,我在子线程发起网络请求后处理返回的数据时,新建了一个Handler去处理。这是因为新建一个Handler是需要在主线程中,主线程是默认循环的,而子线程则不会。 解决方法 有三种方法吧 1 按照提...

杨迪龙的博客 2385

OpenClaw 最强技能 self-improving-agent 详解:让 AI 从错误中自主学习

self-improving-agent 是 OpenClaw 生态中最受欢迎的技能插件,下载量突破 268k。它能让 AI 记住犯过的错误和解决方案,实现持续自我改进。本文深入讲解其工作原理、安装配置和实战案例。测试数据显示:部署流程时间从 15 分钟降到 2 分钟(87%↓),API 调试时间从 2 小时降到 20 分钟,数据库查询编写时间减少 90%。文章包含完整架构图、配置文件示例、3 个详细实战案例和性能测试数据,适合开发者、运维和数据分析师使用。

qq_38610923的博客 3634

Handler 机制源码解析

Handler 最常见的使用场景就是子线程有更新 UI 的需求时,通过 Handler 发送一条消息,然后在主线程中取出这条消息,并更新 UI。我们先从这个过程开始,结合源码看看 Handler 是如何完成这些操作的。 一、流程分析 源码基于 API 30,注释中会标记出所有需要了解的内容,但可能有的代码与本节要分析的工作流程主干部分相关性不大,可以先不看,会摘出来放在后面的章节中再单独说。 看源码之前先介绍一下整个流程中涉及到的角色及其作用: Handler:①发送消息(Message)到消息队列(

tmacfrank 1741

手把手教你用FPGA解析AD9680的JESD204B数据流(附Verilog代码)

本文详细介绍了如何利用FPGA解析AD9680的JESD204B数据流,包括协议核心机制、硬件平台搭建、Verilog代码实现及调试技巧。通过实战案例和代码示例,帮助工程师快速掌握高速ADC数据采集与处理技术,提升系统性能。

weixin_33640562的博客 372

Matlab TreeBagger实战:用随机森林预测房价的5个关键步骤(附完整代码)

本文详细介绍了使用Matlab的TreeBagger工具包构建随机森林模型预测房价的完整流程。通过波士顿房价数据集实战,涵盖数据预处理、模型构建、参数调优、特征重要性分析及高级应用(如分位数回归)等5个关键步骤,并附有完整代码,帮助读者快速掌握这一强大的回归分析工具。

ol789012的博客 444

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

SAP后台JOB保姆级教程:从SM36创建到SM37监控,手把手教你搞定ABAP程序定时任务

本文详细解析SAP后台JOB从创建到监控的全流程,重点介绍SM36创建和SM37监控的操作步骤与最佳实践。通过真实案例演示如何配置定时任务、处理常见故障及实施企业级管理策略,帮助ABAP开发者高效管理后台程序执行。

weixin_30838873的博客 430

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万+

FFmpeg流媒体开发避坑指南:avformat_open_input超时设置的那些坑

本文深入剖析了FFmpeg流媒体开发中avformat_open_input函数的超时设置难题。针对默认阻塞行为、无效的timeout参数以及不稳定的线程取消方案等常见误区,文章详细解读了官方推荐的interrupt_callback机制,并提供了健壮的超时控制器实现方案与最佳实践,帮助开发者构建稳定可靠的流媒体应用。

weixin_29239279的博客 237

全志D1s RISC-V开发板硬件设计与Linux启动全流程解析

RISC-V架构是当前国产嵌入式处理器演进的核心方向,其启动流程涵盖从BootROM、SPL到U-Boot及Linux内核的多级引导机制。理解该流程需掌握DDR初始化、设备树适配、电源时序控制等底层原理,技术价值在于构建可追溯、可调试、可量产的最小可行系统(MVS)。典型应用场景包括边缘AI推理终端、工业HMI和轻量级网关等对成本与开源性敏感的领域。本文以全志D1s平台为载体,深入剖析基于玄铁C906内核的RISC-V SoC在真实硬件上的启动验证、电源完整性设计与Linux全链路开源实践。

weixin_32306683的博客 358

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

Android非UI主线程中,若干普通Java线程使用Handler发送接收消息线程1和线程2均为普通Java线程,在Android中创建,然后在这两个普通Java线程中使用Handler发送和接收消息。import android.os._java handler 非主线程。做一个简单的关于Handler,Looper,Message的小演示,代码主界面一个Button按钮,点击发送消息(累计)给线程的Looper循环,然后在LogCat中打印出来:package zhangphil.looper;

Zhang Phil 2683

22、预条件技术:矩阵分解与多项式预条件器

本文系统介绍了预条件技术中的不完全分裂法、多项式预条件器和近似逆预条件器。内容涵盖矩阵分解的基本原理、不完全LU和Cholesky分解的实现方法、基于填充水平和阈值策略的改进技术,以及多项式和近似逆预条件器的数学基础与应用。通过算法流程图和表格对比,展示了各类预条件器的适用场景与优缺点,同时探讨了其在迭代求解中的关键作用和未来发展方向。

theta的博客 95

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()异常

gaohuanjie的专栏 2万+

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

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

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

Android开发过程中,如果在子线程中创建Handlershi Can't create handler inside thread that has not called Looper.prepare()

欢迎来到“無昂博奥”的博客 966

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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报了一个Can't create handler inside thread that has not calledLooper.prepare()错误。因为toast的实现需要在activity的主线程才能正常工作,所以传统的非主线程不能使toast显示在actvity上,通过Handler。...

IT全栈 华强工作室 8万+

Kotlin报错:Can‘t create handler inside thread that has not called Looper.prepare()

报错: Can’t create handler inside thread that has not called Looper.prepare() 解决方法: 在需要UI控件调用的前后加上prepare()与loop()方法,如下所示: Looper.prepare(); Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show(); Looper.loop(); 有点奇怪的是一般而言使用Java单独在某个类里面(非

kicinio的博客 2951

项目报错:Can‘t create handler inside thread that has not called Looper.prepare()

参考: Can’t create handler inside thread that has not called Looper.prepare() Android 如何判断当前线程是否是主线程子线程中直接toast报错: Can't create handler inside thread that has not called Looper.prepare() 解决方法: if (!N

火星男孩的分享空间 1824

Can‘t create handler inside thread Thread[Thread-13,5,main] that has not called Looper.prepare()

handler异常

weixin_44232136的博客 1547

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

【代码】Can‘t create handler inside thread that has not called Looper.prepare() 解决办法。

chengzhenjia的博客 233

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

FATAL EXCEPTION: OkHttp Dispatcher Can't create handler inside thread that has not called Looper.pre...

qq_45670012的博客 136

Android进阶(八)Can‘t create handler inside thread that has not called Looper.prepare()

Error:Can'tcreatehandlerinsidethreadthathasnotcalledLooper.prepare() 原代码: //利用Handler消息传递机制 finalHandlermyHandler=newHandler(MainActivity.this.getMainLooper()){ @Override public

IT全栈 华强工作室 5万+

安卓子线程吐司问题- Can‘t create handler inside thread that has not called Looper.prepare()

今天又看了看以前写的安卓实验,有一个问题一直没处理好,就是我在网络请求的时候,我根据返回的信息给出一些提示,常见的就是吐司,类似这种,可以更好的和人交互,但是当我把吐司写在网络请求中,程序就会直接崩溃掉,┭┮﹏┭┮ 错误的信息是这样提示的: Can't create handler inside thread that has not called Looper.prepare() 翻译:无法在没有调用 Looper.prepare() 的线程中创建 handler 然后大概就是在子线程中..

m0_52002523的博客 746

Can‘t create handler inside thread Thread[Timer-1,5,main] that has not called Looper.prepare()

Can't create handler inside thread Thread[Timer-1,5,main] that has not called Looper.prepare()

唐诺 1212
上一篇: android 混淆
下一篇: android webRTC 译文
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值