在Android中创建一个内部处理程序

时间:2011-03-28 10:53:20

标签: java android handler toast looper

我试图从线程中显示吐司。

Toast.makeText(activity.getApplicationContext(), "This is the Toast message", Toast.LENGTH_LONG).show();

但抛出异常

java.lang.RuntimeException:Can't create a handler inside thread that has not called Looper.prepare().

我该如何解决这个问题?我已将Looper.myLooper().prepare();放在Toast.makeText(....).show();

之前

当我这样做时,它不会抛出异常,但不会显示任何Toast消息。

那么我该如何解决这个问题?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以在异步任务的onPostExecute中创建Toast消息。试试这个..

protected void onPostExecute(Void result) {
  Toast.makeText(ActivityName.this,"Your Text", Toast.LENGTH_SHORT).show(); 
  if (this.dialog.isShowing()) {
    this.dialog.dismiss();
  }
}
相关问题