未找到Android Textview资源

时间:2016-07-22 09:57:14

标签: java android xml

试图通过大书呆子牧场指南来掌握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;
}

3 个答案:

答案 0 :(得分:2)

试试这个,让我知道结果。 mQuestionTextView.setText(getResources().getString(question));

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

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

答案 1 :(得分:0)

使用此标准代码:

在yout String.xml文件中使用String数组

  <string-array name="questions">
    <item>The Pacific Ocean is larger than the Atlantic Ocean.</item>
    <item>The Suez Canal connects the Red Sea and the Indian Ocean.</item>
    <item>The source of the Nile River is in Egypt.</item>
    <item>The Amazon River is the longest river in the Americas.</item>
    <item>Lake Baikal is the world\'s oldest and deepest freshwater lake.</item>
</string-array>

现在在java类中使用以下代码

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_quiz);
String[] mQuestionBank = getResources().getStringArray(R.array.questions);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
String question = mQuestionBank[mCurrentIndex];
mQuestionTextView.setText(question);

答案 2 :(得分:0)

构造函数的每个参数都会影响对象的一个​​字段 - 在Question类的构造函数中, mTextResId 是构造函数的第一个参数&amp;的本地副本。 mAnswerTrue 是构造函数的第二个参数的本地副本。

要参考问题字段 mTextResId ,构造函数必须使用 this.mTextResId &amp;&amp;的 this.mAnswerTrue 即可。尝试

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