解决在子线程中显示Toast时出现Can‘t toast on a thread that has not called Looper.prepare异常的问题

如果在子线程中直接显示Toast:

Toast.makeText(context, "测试", Toast.LENGTH_SHORT).show();

运行时就会出现如下异常:
java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()

原因是Android中不允许在子线程中处理UI。如果要在子线程中处理UI那就要动态转到主线程中执行,所以常用的3个解决方法如下:

1.使用Looper。

Looper.prepare();
Toast.makeText(context, "测试", Toast.LENGTH_SHORT).show();
Looper.loop();

2.如果代码是在Activity中,可以使用runOnUiThread。

runOnUiThread(new Runnable() {

    @Override
    public void run() {
        Toast.makeText(context, "测试", Toast.LENGTH_SHORT).show();
    }
});

3.使用rxjava。

Observable.just(1L)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(a -> {
            Toast.makeText(context, "测试", Toast.LENGTH_SHORT).show();
        }, throwable -> {
            throwable.printStackTrace();
        });

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Due to hot toxic smoke and unknown risks under fire conditions, detection and relevant reconnaissance are significant in avoiding casualties. A fire reconnaissance robot was therefore developed to assist in the problem by offering important fire information to fire fighters. The robot consists of three main systems, a display operating system, video surveillance, and mapping and positioning navigation. Augmented reality (AR) goggle technology with a display operating system wasalsodevelopedtofreefirefighters’hands,whichenablesthemtofocusonrescuingprocessesand not system operation. Considering smoke disturbance, a thermal imaging video surveillance system wasincludedtoextractinformationfromthecomplicatedfireconditions. Meanwhile,asimultaneous localization and mapping (SLAM) technology was adopted to build the map, together with the help of a mapping and positioning navigation system. This can provide a real-time map under the rapidly changing fire conditions to guide the fire fighters to the fire sources or the trapped occupants. Based on our experiments, it was found that all the tested system components work quite well under the fire conditions, while the video surveillance system produces clear images under dense smoke andahigh-temperatureenvironment;SLAMshowsahighaccuracywithanaverageerroroflessthan 3.43%; the positioning accuracy error is 0.31 m; and the maximum error for the navigation system is 3.48%. The developed fire reconnaissance robot can provide a practically important platform to improve fire rescue efficiency to reduce the fire casualties of fire fighters.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值