简体   繁体   中英

Android Textview resource not found

Trying to get a grasp on android throught The big nerd ranch guide, i came across this example:

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

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

and those have been defined in 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>

However I'm getting a Resource not found Exception. (textResId is the first field of the question class)

EDIT:

Question Class

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

Try this and let me know the result. mQuestionTextView.setText(getResources().getString(question));

Also change the constructor section of the Question class

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

Use this Standard code:

Use String array in yout String.xml file

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

Now use below code in your java class

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

Each argument to the constructor shadows one of the object's fields — inside the constructor of Question class, mTextResId is a local copy of the constructor's first argument & mAnswerTrue is a local copy of the constructor's second argument.

To refer to the Question field mTextResId , the constructor must use this.mTextResId && this.mAnswerTrue . Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM