【Error】Android: java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepa

本文探讨了在Android开发中,于子线程调用Toast显示消息时出现的java.lang.RuntimeException异常。详细分析了错误原因及解决方案,指出应在主线程中正确处理Toast显示,并强调了使用sendMessage而非handleMessage传递消息的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android开发常遇到的错误:java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare():

我在child thread中调用传递message给handler处理,handler中根据message类型来处理,但是遇到了如上的错误。

搜了一下解决方案:

先调用Looper.prepare();
再调用Toast.makeText().show();
最后再调用Looper.loop();

比较奇怪,既然在child thread中发送message给main thread处理了,为什么还会报这个错?暂且记录。

20190423更新:传递message应该使用sendmessage方法而不是handleMessage方法。

参考:

https://www.jianshu.com/p/4551734b3c21

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值