我正在制作一个测验应用程序,而我的Toast Method第一次运行它时,现在不是我改变了一些代码。它现在告诉我,它无法解决Toast方法。我不确定我错过了什么。任何帮助将不胜感激。
submit = (Button) findViewById(R.id.score);
submit.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int correctAnswers = 0;
if (q1.isChecked()) {
correctAnswers += 1;
}
if (q2Box1.isChecked() || q2Box3.isChecked()) {
correctAnswers += 1;
}
if (q3.isChecked()) {
correctAnswers += 1;
}
if (q5.isChecked()) {
correctAnswers += 1;
}
String q4Text = q4.getText().toString();
if (q4Text.equals(String.valueOf(correctAnswers))) {
correctAnswers += 1;
}
// Show score message as a toast
Toast.makeText(this, "You got " + correctAnswers + "/5 correct!", Toast.LENGTH_LONG).show();
// Exit this method early because there's nothing left to do
return;
}
});}}
答案 0 :(得分:0)
您更改了范围this
是OnClickListerner
。
使用getApplicationContext()
- >
Toast.makeText(getApplicationContext(), "You got " + correctAnswers + "/5 correct!", Toast.LENGTH_LONG).show();
答案 1 :(得分:0)
试试这个:
Toast.makeText(YourActivity.this, "You got " + correctAnswers + "/5 correct!", Toast.LENGTH_LONG).show();