[英]Create a Handler Inside Thread in Android
I tried to show the toast from the thread. 我试图从线程中显示吐司。
Toast.makeText(activity.getApplicationContext(), "This is the Toast message", Toast.LENGTH_LONG).show();
but throws the exception 但引发异常
java.lang.RuntimeException:Can't create a handler inside thread that has not called Looper.prepare().
How do I solve this problem? 我该如何解决这个问题? I have put the
Looper.myLooper().prepare();
我把
Looper.myLooper().prepare();
before the Toast.makeText(....).show();
在
Toast.makeText(....).show();
When I have done this it doesn't throw the exception but does not show any toast message. 完成此操作后,它不会引发异常,但不会显示任何吐司消息。
So how do I solve this problem? 那么我该如何解决这个问题呢?
Thanks in advance. 提前致谢。
You can create your Toast message in onPostExecute of Async Task.. Try this.. 您可以在异步任务的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.