Reputation: 11
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;
}
Upvotes: 0
Views: 215
Reputation: 3455
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;
}
Upvotes: 0
Reputation: 1123
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;
}
Upvotes: 2
Reputation: 573
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);
Upvotes: 0