Android,在onClickListener中使用Toast [英] Android, using Toast inside onClickListener
问题描述
在onClickListener内的某些条件下,我试图使吐司显示一些文本.该应用程序将无法在模拟器中运行,并且出现以下错误:"void无法转换为Toast"
I am trying to make a toast display some text given certain conditions inside an onClickListener. The app won´t run in the simulator, and I get the following error: "void cannot be converted to Toast"
我在此论坛上进行了搜索,发现了一些类似的问题和解决方案,但没有一个完全适用于我的问题.其他人在声明中没有使用正确的上下文,但是我的意思是我确实使用了. (javafile(上下文)的名称是:"Case1Activity".有人可以帮助我吗? 我对代码进行了一些简化:
I have searched around, and find several similar problems and solutions on this forum, but none of them applies completely to my problem. The others haven´t used the correct context in the statement, but I really mean that I do. (The name of the javafile (context) is: "Case1Activity") Can anyone help me with this? I have simplified the code a bit:
public void onClick(View view) {
if (button1Pushed == false){
count++;
Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
}
}
});
推荐答案
无需赋值语句即可完成
Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
这篇关于Android,在onClickListener中使用Toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!