开发者

Create a Handler Inside Thread in Android

开发者 https://www.devze.com 2023-02-20 10:34 出处:网络
I tried to show the toast from the thread. Toast.makeText(activity.getApplicationContext(), \"This is the Toast message\", Toast.LENGTH_LONG).show();

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(); before the 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 s开发者_开发百科olve this problem?

Thanks in advance.


You can create your Toast message in onPostExecute of Async Task.. Try this..

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

精彩评论

暂无评论...
验证码 换一张
取 消