简体   繁体   English

在Android中创建线程内的处理程序

[英]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.

相关问题 Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android 警报对话框崩溃Android测试 - “无法在未调用Looper.prepare()的线程内创建处理程序” - Alert Dialog crashes Android test - “Can't create handler inside thread that has not called Looper.prepare()” Android上的Junit测试领域。 无法在未调用Looper.prepare()的线程内创建处理程序 - Junit Testing Realm on Android. Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()的线程内创建处理程序-Android Marmalade - Can't create handler inside thread that has not called Looper.prepare() - Android Marmalade 无法在未调用Looper.prepare()Android的线程内创建处理程序。 循环计时器 - Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer Android后台服务错误:无法在未调用的线程内创建处理程序 - Android Background Service Error : Can't create handler inside thread that has not called Android空指针异常,并且无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android nullpointer exception and Can't create handler inside thread that has not called Looper.prepare() 无法在尚未调用looper.prepare android的线程内创建处理程序 - cant create handler inside thread that has not called looper.prepare android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM