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方法。
参考: