Toast 在侦听器中不起作用
我想打开一个 Toast,但它似乎在(Smack 的)监听器中不起作用,就像这个例子:
private ChatManagerListener chatManagerListener = new ChatManagerListener() {
public void chatCreated(Chat newChat, boolean createdLocally) {
if(!createdLocally) {
Toast.makeText(mCtx, "CHAT CREATED", Toast.LENGTH_SHORT).show();
// do some stuff
}
}
};
如果我在类中的其他地方使用 Toast 代码,它就可以工作,只要它不是一个倾听者。
I want to open a Toast, but it doesn"t seem to work in a listener (of Smack), like this example:
private ChatManagerListener chatManagerListener = new ChatManagerListener() {
public void chatCreated(Chat newChat, boolean createdLocally) {
if(!createdLocally) {
Toast.makeText(mCtx, "CHAT CREATED", Toast.LENGTH_SHORT).show();
// do some stuff
}
}
};
If I use the Toast-code somewhere else in the class, it works, as long as it isn't a listener.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您分配了适当的上下文。您的 mCtx 应该是父类的上下文。尝试像这样分配它:ParentClassName.this
Make sure you're assigning the appropriate Context. Your mCtx should be the context of the parent class. Try assigning it like this: ParentClassName.this
您确定它位于 if 条件内吗?
如果是这样,那么我认为您没有了解那里的背景。
Are you sure that it goes inside the if condition?
If so, then I think you are not getting the context there.