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

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

发布了242 篇原创文章 · 获赞 95 · 访问量 61万+

猜你喜欢

转载自blog.csdn.net/think_ycx/article/details/89454925