2016-07-22 70 views
0

试图让机器人把握throught大书呆子牧场指导,我遇到了这个例子:的Android TextView的资源没有发现

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

什么是mCurrentIndex? –

+0

发布您的logcat输出 –

+0

mCurrentIndex是一个私人int = 0 – Boulbi

回答

0

使用该标准代码:

使用字符串数组中YOUT String.xml文件

<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

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

也改变了问题类

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

这导致相同的输出。 – Boulbi

+0

请分享分享“问题”类 – lal

+0

请更改问题分类中的构造函数 public Question(int mTextResId,boolean mAnswerTrue){this.mTextResId = mTextResId; this.mAnswerTrue = mAnswerTrue; } – lal

0

每个参数的构造函数的阴影对象的领域之一的构筑部 - 问题类的构造函数中,mTextResId是构造的的本地副本第一个参数& mAnswerTrue是构造函数第二个参数的本地副本。

来指代问题mTextResId,构造函数必须使用this.mTextResId & & this.mAnswerTrue。尝试

public Question(int mTextResId, boolean mAnswerTrue) { 
    this.mTextResId = mTextResId; 
    this.mAnswerTrue = mAnswerTrue; 
} 
相关问题