Android在thread中Toast不能显示问题解决

来源:互联网 发布:软件巡检方案 编辑:程序博客网 时间:2024/06/11 23:49

第一种方法:

改写代码之前是:

Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();
改写之后:

Looper.prepare();Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();Looper.loop();

一般如果不是在主线程中又开启了新线程的话,一般都会碰到这个问题。

原因是在创建新线程的时候默认情况下不会去创建新的MessageQueue。


第二种方法:

Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubif (msg.what == 0) {Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();}super.handleMessage(msg);}};Message msg = handler.obtainMessage();msg.what = 0;handler.sendMessage(msg);


线程里面不能进行UI操作的,可以在线程里面用handler发送信息,然后再显示UI,比如就把你的toast改成handler.sendEmptyMessage()。。

0 0