无法解析Toast方法(Android Studio)

时间:2018-03-11 21:37:23

标签: java android-studio

我正在制作一个测验应用程序,而我的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;
        }
        });}}

2 个答案:

答案 0 :(得分:0)

您更改了范围thisOnClickListerner。 使用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();