如何在 Android 上正确使用 Toast 通知?

发布于 2024-12-19 00:33:32 字数 642 浏览 2 评论 0原文

我似乎无法正确使用 toast 通知。在我的所有其他应用程序中,它都运行得很好,但在这个应用程序中却不然。在这个应用程序中,我开始使用 openGL 和一本名为“Beginning Android Games”的书中的框架,现在我似乎无法使用 toast 通知。我不知道该怎么办...由于上下文而失败。我怎样才能创建一个有效的上下文?请帮我!这是我代码的一部分,因为代码太长:

private void updateReady() {
Coin.number = 0;
if (game.getInput().getTouchEvents().size() > 0) {
    state = GAME_RUNNING;
    Coin.number = 0;
    Num.number = 0;

    Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();
    }
}

当我将行:

    Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();

放在扩展 Activity 的类中并运行它时,它只是不执行任何操作...我尝试将其放入方法中并从中调用它其他课程,但它被强行关闭......

I can't seem to be able to use the toast notification properly. In all of my other apps it worked great but in this one it doesn't. In this app I started using openGL with a framework from a book named "Beginning Android Games" and now I don't seem to be able to use the toast notification. I have no idea what to do... It fails because of the context. How can I make a context that will work? Please help me! this is part of my code because the code is too long:

private void updateReady() {
Coin.number = 0;
if (game.getInput().getTouchEvents().size() > 0) {
    state = GAME_RUNNING;
    Coin.number = 0;
    Num.number = 0;

    Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();
    }
}

When I put the line:

    Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();

in the class that extends Activity and run it it just doesn't do anything... I tried to make it into a method and call it from other classes but it got a force close...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月蹉跎了容颜 2024-12-26 00:33:32

您可以尝试使用 getApplicationContext() 获取对当前 Activity 上下文的引用

You can try using getApplicationContext() to get a reference to the current Activity context

微凉徒眸意 2024-12-26 00:33:32

您始终可以使应用程序上下文或启动 GameScreen 的任何活动的上下文静态可用,或将其作为创建实例的参数传递。

尽管如此,小心内存泄漏

You could always make your application context, or the context of whatever activity you're starting your GameScreen from, available statically or by passing it as an argument from whatever creates your instance.

Having said that, though, beware of memory leaks!

花开柳相依 2024-12-26 00:33:32

也许这有帮助。

我有这样定义的类

    public class tutorialThree extends Activity implements View.OnClickListener

我尝试像这样使用Toast

    Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();

没有工作,因为我的类实现了该接口“View.OnClickListener”(或任何它是什么:))
因此,toast 与“this”引用混淆,您必须更精确,因此在“this”关键字之前添加类的名称,如下所示:

    Toast.makeText(tutorialThree.this, "Wallpaper set", Toast.LENGTH_SHORT).show();

这解决了我的问题,现在我可以看到 toast。

Maybe this helps.

I had class defined like this

    public class tutorialThree extends Activity implements View.OnClickListener

I tried to use Toast like this

    Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();

Did not work, because my class implements that interface "View.OnClickListener" (or whatever it is :))
So toast gets confused with "this" reference, you have to be more precise, so add name of your class before "this" keyword, like this:

    Toast.makeText(tutorialThree.this, "Wallpaper set", Toast.LENGTH_SHORT).show();

This solved my problem, now i can see toast.

硬不硬你别怂 2024-12-26 00:33:32

我假设您正在使用名为“Beginning Android Games 2”。

根据此code,这里需要的实例变量是glGame,它是一个GLGame 对象。它扩展了Activity,所以你可以这样做:

Toast.makeText(glGame, "Start!", Toast.LENGTH_SHORT).show();

I assume you're working with this framework called "Beginning Android Games 2".

According to this code, the instance variable you need here is glGame, which is a GLGame object. It extends Activity, so you can just do this:

Toast.makeText(glGame, "Start!", Toast.LENGTH_SHORT).show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文