gpt4 book ai didi

java - Android Textview 资源未找到

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

试图通过《大 Nerd 牧场指南》来掌握 Android,我遇到了这个例子:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);

mQuestionBank 所在位置

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

这些已在 strings.xml 中定义

<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>

但是我遇到了资源未找到异常。(textResId是问题类的第一个字段)

编辑:

问题类别

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

public Question(int mTextResId, boolean mAnswerTrue) {
mTextResId=mTextResId;
mAnswerTrue=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;
}

最佳答案

尝试一下并让我知道结果。 mQuestionTextView.setText(getResources().getString(question));

同时更改 Question 类的构造函数部分

public Question(int mTextResId, boolean mAnswerTrue) {
this.mTextResId=mTextResId;
this.mAnswerTrue=mAnswerTrue;
}

关于java - Android Textview 资源未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38523491/

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