天天看點

解決 No resource found that matches the given name (at 'layout_above' with value '@id/button3').

今天繼續學習郭少的<<第一行代碼>>,讀到3.3.2相對布局這一章(P102頁),根據書上的執行個體,運作以後在控制台顯示了,

Information:Gradle tasks [:app:assembleDebug]

C:\Users\Administrator\AndroidStudioProjects\UILayoutTest\app\src\main\res\layout\activity_main.xml

Error:(10, 31) No resource found that matches the given name (at 'layout_above' with value '@id/button3').

Error:(11, 34) No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/button3').

Error:(17, 31) No resource found that matches the given name (at 'layout_above' with value '@id/button3').

Error:(18, 35) No resource found that matches the given name (at 'layout_toRightOf' with value '@id/button3').

好你是說沒有找到與‘@id/button3‘比對的名字的資源,怎麼回事呢,仔細看了一下activity_main.xml,發現了問題

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button3"
android:layout_toLeftOf="@id/button3"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button3"
android:layout_toRightOf="@id/button3"
android:text="Button 2" />      
<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Button 3" />      
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"
android:text="Button 4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button3"
android:layout_toRightOf="@id/button3"
android:text="Button 5" />      

最後讀到第103頁,上面清楚的寫到。注意,當一個控件去引用另一個控件的id時,該控件一定要定義在引用控件的後面,不然會出現找不到id的情況。将button3寫到中間了,将button3的位置放到button1上面就行了,期待大家不要犯同樣的錯誤!