android: toast报错 :Can't toast on a thread that has not called Looper.prepare()

一,报错信息:

java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()

原因:

原因是Android中不允许在子线程中处理UI。
如果要在子线程中处理UI那就要动态转到主线程中执行,
解决方法:使用Looper

二,解决:

代码如下:

            Looper.prepare();
            Toast.makeText(context, "消息内容:状态正常", Toast.LENGTH_SHORT).show();
            Looper.loop();

代码前后分别添加了:

            Looper.prepare();

            Looper.loop();

 

posted @ 2025-06-14 15:22  刘宏缔的架构森林  阅读(234)  评论(0)    收藏  举报