来自BigNerdRanch的GeoQuiz应用程序没有按预期工作

时间:2016-06-01 18:12:49

标签: java android toast

我是Android新手,我正在开发“Android编程大书呆子牧场指南V2”一书中的GeoQuiz应用程序,在完全阅读本书(第1章)后,我制作了测验应用程序。但它没有显示Toasts的正确和错误的答案!我肯定做错了!!,有人可以帮我解决。

public class MainActivity extends AppCompatActivity {

    private Button mTrueButton,mFalseButton,mNextButton;
    private TextView mQuestionTextView;

    private Question[] mQuestionBank= new Question[]{
            new Question(R.string.question_oceans, true),
            new Question(R.string.question_mideast, false),
            new Question(R.string.question_africa, false),
            new Question(R.string.question_americas, true),
            new Question(R.string.question_asia, true)
    };

    private int mCurrentIndex=0;

    private void updateQuestion(){
        int question=mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }

    public void checkAnswer(boolean userPressedTrue){
         boolean answerIsTrue=mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId=0;

        if(userPressedTrue == answerIsTrue){
            messageResId=R.string.correct_toast;
        }
        else
        {
            messageResId=R.string.incorrect_toast;
        }

        Toast.makeText(getApplicationContext(), messageResId, Toast.LENGTH_SHORT);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNextButton=(Button)findViewById(R.id.next_button);

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });


        mQuestionTextView=(TextView)findViewById(R.id.question_text_view);

        mTrueButton=(Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });

        mFalseButton=(Button)findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });
        updateQuestion();
    }
}

这是问题活动

public class Question {
    private int mTextResId;
    private boolean mAnswerTrue;

    public Question(int textResId, boolean answerTrue){
        mTextResId=textResId;
        mAnswerTrue=answerTrue;
    }

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public int getTextResId() {
        return mTextResId;
    }

    public void setTextResId(int textResId) {
        mTextResId = textResId;
    }

    public void setAnswerTrue(boolean answerTrue){
        mAnswerTrue=answerTrue;
    }
}

1 个答案:

答案 0 :(得分:0)

您正在创建Toast,但不是show。拨打show()并查看,如下所示:

Toast.makeText(getApplicationContext(), messageResId, Toast.LENGTH_SHORT).show();