Java Android Toast 消息不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20678436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:32:59  来源:igfitidea点击:

Android Toast Messages not working

javaandroidandengineandroid-toast

提问by user3076301

I'm developing a game via Andengine for Android. I have MainActivity class and GameScene class. I use Toast messages in GameActivity. And it is working.

我正在通过 Andengine for Android 开发游戏。我有 MainActivity 类和 GameScene 类。我在 GameActivity 中使用 Toast 消息。它正在发挥作用。

Toast.makeText(this, " Hello World", Toast.LENGTH_SHORT).show();

So I wanna use Toast messages in GameScene class. But it doesn't work. Here is the code:

所以我想在 GameScene 类中使用 Toast 消息。但它不起作用。这是代码:

Toast.makeText(activity, " Hello World", Toast.LENGTH_SHORT).show();

I have to use "activity" instead of "this". But it doesn't work

我必须使用“活动”而不是“这个”。但它不起作用

why?

为什么?

EDITED:

编辑:

when I use second one, an error occurs. LogCat: http://s29.postimg.org/k8faj9mdj/Capture.png

当我使用第二个时,发生错误。LogCat:http: //s29.postimg.org/k8faj9mdj/Capture.png

采纳答案by laalto

You're trying to display a Toastin a background thread. You should do all your UI operations on the main UI thread.

您正在尝试Toast在后台线程中显示 a 。您应该在主 UI 线程上执行所有 UI 操作。

The exception RuntimeException: Can't create handler inside thread that has not called Looper.prepare()can be a little cryptic for beginners but essentially it tells you that you're in a wrong thread.

RuntimeException: Can't create handler inside thread that has not called Looper.prepare()对于初学者来说,这个例外可能有点神秘,但本质上它告诉你你在一个错误的线程中。

To solve it, wrap the toast to e.g. runOnUiThread():

要解决它,请将吐司包装到例如runOnUiThread()

activity.runOnUiThread(new Runnable() {
  @Override
  public void run() {
    Toast.makeText(...).show();
  }
});

回答by Avijit

Use:

用:

Toast.makeText(getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();

or

或者

Toast.makeText(activity.this, " Hello World", Toast.LENGTH_SHORT).show();

回答by balaji koduri

Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();

try this.

尝试这个。

回答by mehmetseckin

Since you asked why; i think you're giving an activity reference as a context to the Toast message, this is why it isn't working.

既然你问为什么;我认为您将活动引用作为 Toast 消息的上下文,这就是它不起作用的原因。

If you're trying to show a Toast message from outsideof an activity, you could try :

如果您尝试从活动外部显示 Toast 消息,您可以尝试:

Toast.makeText(activity.getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();

or from the GameActivity

或从 GameActivity

Toast.makeText(GameActivity.this, " Hello World", Toast.LENGTH_SHORT).show();

or from the MainActivity

或从 MainActivity

Toast.makeText(MainActivity.this, " Hello World", Toast.LENGTH_SHORT).show();

回答by Ilya Gazman

There could be two reasons for your code to not work. It's ether your activity parameter is null or...

您的代码不起作用可能有两个原因。您的活动参数是否为空或...

Short time after you showing the toast the activity is die, in that case it will kill the toast as well, to avoid this you can call activity.getApplicationContext()like in @Mehmet Se?kin answer.

在您展示 toast 后不久,活动就会消失,在这种情况下,它也会杀死 toast,为了避免这种情况,您可以activity.getApplicationContext()像在@Mehmet Se?kin 回答中那样调用。

回答by Androidian

use one of the following

使用以下之一

Toast.makeText(getApplicationContext(), " Hello World", Toast.LENGTH_SHORT).show();

        Toast.makeText(getBaseContext(),"please Create your Account First", Toast.LENGTH_SHORT).show();


      Toast.makeText(GameActivity.this,"please Create your Account First", Toast.LENGTH_SHORT).show();

回答by Guru

Since You are calling it from the class. you need to get the context from the activity through the class constructor or else you need to use GetApplicationcontext().

因为你是从课堂上调用它的。您需要通过类构造函数从活动中获取上下文,否则您需要使用 GetApplicationcontext()。

回答by Juan Mendez

Make sure the app you are testing has notifications turned on. That was my story and why toasts weren't working either. I had gone looking for a straight answer and it just happens that toasts are considered part of the notifications. Interesting stuff, I had no clue.

确保您正在测试的应用程序已打开通知。这就是我的故事,也是为什么 toast 也不起作用。我一直在寻找一个直接的答案,碰巧的是,吐司被认为是通知的一部分。有趣的东西,我不知道。