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()

 

posted @ 2014-04-30 11:22  等风来。。  Views(391)  Comments(0Edit  收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------