如何在非活动课程中祝酒?
我有一个类,用于在我的活动中获取 GPS 数据。在构造函数中,我将活动的上下文传递给它:
gpsFetcher = new GPSFetcher(this);
在 gpsFetcher 类中,我有:
this.context = c.getApplicationContext();
或者
this.context = c;
然后我用以下命令调用 toast:
Toast.makeText(context, "sometext", Toast.LENGTH_LONG);
但它永远不会显示...我缺少什么吗?是否可以?
谢谢!
I have a class that I am using to get GPS data within my activity. In the constructor I pass it the activity's context:
gpsFetcher = new GPSFetcher(this);
and in the gpsFetcher class I have:
this.context = c.getApplicationContext();
OR just
this.context = c;
and then I call the toast with:
Toast.makeText(context, "sometext", Toast.LENGTH_LONG);
But it never shows up... Is there something I'm missing? Is it possible?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否忘记了
Toast#show
?Are you forgetting
Toast#show
?您还必须调用
show()
:Toast.makeText(context, "sometext", Toast.LENGTH_LONG).show();
You must call
show()
as well:Toast.makeText(context, "sometext", Toast.LENGTH_LONG).show();
我也遇到了同样的问题,不过我解决了!!在非活动类中,您只需声明一个“公共静态字符串”。然后在你的MainActivity或者其他activity中,你可以直接使用Toast。
就我而言,我声明了一个非活动类 NoteDB。所以我在类中声明
public static String S
。 (您可以在类中更改 S 值。然后在我的 MainActivity 中,我宣布它运行良好。
I have met the same question but I solved it.!! In the non-activity class , you just announce a "public static String". Then in your MainActivity or other activity , you can directly use Toast.
In my case , I declare a non-activity class NoteDB. so I declare
public static String S
in the class . (You can change S value in the class. Then in my MainActivity , I announceIt works well.
要在非活动 Java 类中显示 Toast,请在 java 类的构造函数中添加 Context
[这里,PriceMethods 是我的 java 类]
,以及在活动中实例化此类的位置(创建它的对象,在中使用它)您的主要活动),添加上下文作为参数。
像这样:
在你的java类中之后,你可以创建一个像这样的toast:
To display a Toast in a Non-Activity Java class, add the Context in the constructor of the java class
[Here PrizeMethods is my java class]
and where you are instancing this class in your activity (making an object of it, using it in your main activity), add the context as a parameter.
Like this:
after that inside your java class, you can create a toast like this: