![](/img/trans.png)
[英]Android thread error - Can't create handler inside thread that has not called Looper.prepare()
[英]Create a Handler Inside Thread in Android
我试图从线程中显示吐司。
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();
完成此操作后,它不会引发异常,但不会显示任何吐司消息。
那么我该如何解决这个问题呢?
提前致谢。
您可以在异步任务的onPostExecute中创建Toast消息。
protected void onPostExecute(Void result) {
Toast.makeText(ActivityName.this,"Your Text", Toast.LENGTH_SHORT).show();
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.