我是靠谱客的博主 忧伤小兔子,这篇文章主要介绍Android Can‘t toast on a thread that has not called Looper.prepare(),现在分享给大家,希望可以做个参考。

错误

Can't toast on a thread that has not called Looper.prepare()
1

错误的场景

  • 当时 Activity 为销毁状态,只有两个服务线程在后台运行,中间运用 socket.io 通讯,
    当接收(on)消息时需要运用 Toast.makeText 进行通知。

  • 原因是:Android是不能直接在子线程中弹出Toast的

  • 子线程中弹出Toast,以及一些其他类似的子线程中操作的错误。

解决方法

		  //解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            Looper.loop();

最后

以上就是忧伤小兔子最近收集整理的关于Android Can‘t toast on a thread that has not called Looper.prepare()的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(141)

评论列表共有 0 条评论

立即
投稿
返回
顶部