[英]Android Textview resource not found
Trying to get a grasp on android throught The big nerd ranch guide, i came across this example: 试图掌握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);
where mQuestionBank 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 那些已经在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) (textResId是问题类的第一个字段)
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 还要更改Question类的构造器部分
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.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>
Now use below code in your java class 现在在您的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);
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. 构造函数的每个参数都遮蔽了对象的一个字段-在Question类的构造函数内部, mTextResId是构造函数的第一个参数的本地副本,而mAnswerTrue是构造函数的第二个参数的本地副本。
To refer to the Question field mTextResId , the constructor must use this.mTextResId && this.mAnswerTrue . 要引用问题字段mTextResId ,构造函数必须使用this.mTextResId && this.mAnswerTrue 。 Try 尝试
public Question(int mTextResId, boolean mAnswerTrue) {
this.mTextResId = mTextResId;
this.mAnswerTrue = mAnswerTrue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.