2013-05-19 69 views
5

匹配在這個文件是我的佈局下面的錯誤在此代碼錯誤:沒有資源發現在給定的名字

error: Error: No resource found that matches the given name (at 'id' with value '@id/question_text_view'). 
error: Error: No resource found that matches the given name (at 'id' with value '@id/next_button'). 

顯示這是佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@id/question_text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="24dp" 
     /> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:orientation="horizontal" > 
     <Button 
      android:id="@+id/true_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/true_button" 
     /> 
     <Button 
      android:id="@+id/false_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/false_button" 
     /> 
    </LinearLayout> 
     <Button 
     android:id="@id/next_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/next_button" 
     /> 
</LinearLayout> 

這是我的琴絃。 xml

<?xml version="1.0" encoding="utf-8"?> 
    <resources> 

     <string name="app_name">GeoQuiz</string> 
     <string name="true_button">True</string> 
     <string name="false_button">False</string> 
     <string name="correct_toast">Correct</string> 
     <string name="next_button">Next</string> 
     <string name="incorrect_toast">Incorrect</string> 
    <string name="action_settings">Settings</string> 
    <string name="question_founder">Frank Reed Horton Founded APO on december 25 1947</string> 
    <string name="question_chief">Willy Wonka was known as a cheif</string> 
    <string name="question_lady">The first lady that became president in APO was Mary Katz</string> 
    <string name="question_president">Our current President of the Delta Rho chapter is john dolan</string> 
<string name="question_alphabets">Alpha, Beta, Gamma, Delta, Epsilon, Eta, Zeta</string> 
</resources> 

我知道有關於字符串的知道question_text_view,但我確實在我的活動中創建了一個數組這需要所有的問題 mQuestionTextView =(TextView)findViewById(R.id.question_text_view); int question = mQuestionBank [mCurrentIndex] .getQuestion(); mQuestionTextView.setText(question); mQuestionBank是的,我問 getQuestion()的所有問題陣列是我得到的問題

和next_button我不知道我做錯了什麼,因爲我把它上的strings.xml 第二誤差getter方法任何人都可以請我幫助我

+1

機器人:ID = 「@ ID/question_text_view」 應該是機器人:ID = 「@ + ID/question_text_view」 相同爲按鈕機器人:id =「@ + id/next_button」 – Raghunandan

+1

plz see [更多資源類型](h ttp://developer.android.com/guide/topics/resources/more-resources.html#Id)在xml中爲Views定義id。您需要使用''而不是''來將id放置在strings.xml文件中,或者您可以爲它創建單獨的文件,如ids.xml –

回答

7

機器人:ID = 「@ ID/question_text_view」 應該是機器人:ID = 「@ + ID/question_text_view」 相同爲按鈕機器人:ID = 「@ ID/next_button」應該是機器人:ID = 「@ + ID/next_button」

的TextView

<TextView 
    android:id="@+id/question_text_view" // missing + in your xml code 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="24dp" 
    /> 

按鈕

<Button 
    android:id="@+id/next_button" // missing + in your xml code 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/next_button" 
    /> 

ID

任何View對象都可能有一個與它關聯的整數ID,以唯一標識樹中的View。在編譯應用程序時,此ID作爲整數引用,但ID通常在佈局XML文件中以字符串形式在id屬性中分配。這是所有View對象(由View類定義)共有的XML屬性,您將經常使用它。 XML標記內的ID的語法如下:

字符串開始處的at符號(@)表示XML解析器應解析並展開ID字符串的其餘部分,並將其標識爲ID資源。加號(+)表示這是一個新的資源名稱,必須創建並添加到我們的資源(在R.java文件中)

在你的情況下,你認爲按鈕的id是android:id =「@ id/next_button」。您正在引用Android資源ID(假設)。這不存在。對於textview也是一樣。因此你得到資源未找到錯誤。

欲瞭解更多信息,請查看下面

http://developer.android.com/guide/topics/ui/declaring-layout.html

0

你應該小心使用ID的意見。在你的TextView和你的按鈕視圖中,你忘記了ID屬性中的'+'。據我所知,你嘗試用ID來引用另一個視圖。如果您使用'+',則爲該視圖創建一個新ID。

修正版本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/question_text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="24dp" 
     /> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:orientation="horizontal" > 
     <Button 
      android:id="@+id/true_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/true_button" 
     /> 
     <Button 
      android:id="@+id/false_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/false_button" 
     /> 
    </LinearLayout> 
     <Button 
     android:id="@+id/next_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/next_button" 
     /> 
</LinearLayout> 
0

鏈接可能的文件夾路徑太長 比如我的「E:\項目\手機\ SixworldGit \ sixworld \項目\ sixworld \ SW_Vietnam \ 2.3。 2.0.20160711_Android_MoboSDKAll_lucgioi_20160711_1805 \ 2.3.2.0.20160711_Android_MoboSDKAll \ MoboSDK \參考\ MoboSDKAll \ RES \可拉伸」

相關問題