Android Studio setText(int)

时间:2018-04-05 10:55:17

标签: java android

我是Android Studio的新手,我从一本书中学习。有一步一步的课程,最后你将有一个Android应用程序。我没有得到任何错误在IDE中,但是当我尝试在我的设备上模拟这个时,应用程序崩溃了,在日志事件中我可以看到错误在行" mQuestionTextView.setText(question);"

如果我将删除此行应用程序启动但没有text.If我将在TextView中使用 String.valueOf(int) Integer.toString() 0

同样在日志事件中:
"引起:java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(int)'在空对象引用"

有人可以帮我解决这个问题吗?

由于

QuizActivity.java

public class QuizActivity extends AppCompatActivity {

private Button mTBtn;
private Button mFBtn;
private Button mNBtn;
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;

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

    updateQuestion();

    mTBtn = (Button) findViewById(R.id.tBtn);
    mFBtn = (Button) findViewById(R.id.fBtn);
    mNBtn = (Button) findViewById(R.id.nBtn);
    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

    mTBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(QuizActivity.this, R.string.incorrect, Toast.LENGTH_SHORT).show();
        }
    });

    mFBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(QuizActivity.this, R.string.correct, Toast.LENGTH_SHORT).show();
        }
    });

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

}

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

Question.java

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

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

public int getTextResId() {
    return mTextResId;
}

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

public boolean isAnswerTrue() {
    return mAnswerTrue;
}

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

的strings.xml

<resources>
<string name="app_name">GeoQuiz</string>
<string name="true_button">TRUE</string>
<string name="false_button">FALSE</string>
<string name="next_button">NEXT</string>
<string name="correct">Correct!</string>
<string name="incorrect">Incorrect!</string>
<string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longest river in the Americas.</string>
<string name="question_asia">Lake Baikal is the world\’s oldest and deepest freshwater lake.</string></resources>

EDIT1 问题是Question.java

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

我已经改变了变量的位置

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

感谢所有帮助过我的人!

0 个答案:

没有答案