Handler 与 Toast

Toast或者Dialog中都有一个Handler的成员变量,所以如果不是在主线程中使用Toast或Dialog,则需要在使用Toast或者Dialog的线程中初始化Looper。

Looper.prepare();
Toast.makeText(MainActivity.this, "网络出错了", Toast.LENGTH_SHORT).show();
Looper.loop();

否则出现异常:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

原文地址:https://www.cnblogs.com/yuyutianxia/p/3700924.html