2016-07-22 229 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; 
} 
相關問題