簡體   English   中英

找不到Android Textview資源

[英]Android Textview resource not found

試圖掌握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);

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

試試這個,讓我知道結果。 mQuestionTextView.setText(getResources().getString(question));

還要更改Question類的構造器部分

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

使用此標准代碼:

在您的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>

現在在您的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);

構造函數的每個參數都遮蔽了對象的一個​​字段-在Question類的構造函數內部, mTextResId是構造函數的第一個參數的本地副本,而mAnswerTrue是構造函數的第二個參數的本地副本。

要引用問題字段mTextResId ,構造函數必須使用this.mTextResId && this.mAnswerTrue 嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM