gpt4 book ai didi

java - 无法解决 makeText 方法

转载 作者:行者123 更新时间:2023-12-02 13:13:01 25 4
gpt4 key购买 nike

我是开发新手,我买了一本书,《Big Nerd Ranch Android 编程指南》。我完成了第 2 章,这个应用程序一切正常,它向我展示了如何制作。现在我试图完成最后的挑战,但行不通。它给了我一个我根本没有改变的东西的错误,并且它昨天起作用了。该挑战要求我添加一个“上一个”按钮。

“无法解决方法‘makeText(anonymous android.view.View.OnClickListener, int, int)’

错误位于我尝试创建 Toast 的最底部。

package com.example.lthol.geoquiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

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

private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_australia, true),
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;

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

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);
}
});

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

mPrevButton = (Button) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();

}
});

updateQuestion();
}

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

private 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(this, messageResId, Toast.LENGTH_SHORT)
.show();
}
}

最佳答案

mPrevButton onClick 监听器实际上包含在 mNextButton 实现中。您需要关闭支架以使它们分开。

关于java - 无法解决 makeText 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43877046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com