Android AAPT Problem——No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/ib_channel6').

Android 4.0

No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/ib_channel6').

错误代码:
 <!-- channel5 -->
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/ib_channel6"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/ib_channel6"
            android:background="@drawable/channel5" >
        </ImageButton>
        <!-- channel6 -->
        <ImageButton
            android:id="@+id/ib_channel6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/ib_channel7"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="35dp"
            android:background="@drawable/channel6" >
        </ImageButton>
        <!-- channel7 -->
        <ImageButton
            android:id="@+id/ib_channel7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="12dp"
            android:background="@drawable/channel7" >
        </ImageButton>
问题提示:
在Problems的错误提示:
Description    Resource    Path    Location    Type
error: Error: No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/ib_channel6').    activity_youku.xml    /YoukuMenu/res/layout    line 128    Android AAPT Problem
error: Error: No resource found that matches the given name (at 'layout_above' with value '@id/ib_channel6').    activity_youku.xml    /YoukuMenu/res/layout    line 128    Android AAPT Problem
error: Error: No resource found that matches the given name (at 'layout_above' with value '@id/ib_channel7').    activity_youku.xml    /YoukuMenu/res/layout    line 140    Android AAPT Problem
分析错误是由于在xml编译期,<!-- channel5 -->写在 <!-- channel6 -->前面<!-- channel6 -->写在 <!-- channel7-->的前面,编译的时候 RelativeLayout 的ID @+id/ib_channel6和@+id/ib_channel7未添加到系统中,而channel5 就提前使用了这条ID资源 android:layout_above="@id/test_bottom_buttons" 因此编译到此处时,由于找不到ID资源而导致编译错误并停止编译,于是就出现了上述“莫名其妙”的错误。解决办法之一是,严格遵循“先声明后使用”的原则办事。
解决1:使用的id要写在前面,遵循先声明后使用的原则,也就是说channel5使用到了channel6的id,channel6使用到了channel7的id,所以channel6要在channel5之前声明,否则在channel5使用channel6的id的时候找不到channel6的id,channel6使用channel7的id情况也一样,所以综上,channel7要在channel6之前,channel6要在channel5之前。
<!-- channel7 -->
<ImageButton
    android:id="@+id/ib_channel77"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="12dp"
    android:background="@drawable/channel7" >
</ImageButton>
<!-- channel6 -->
<ImageButton
    android:id="@+id/ib_channel6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/ib_channel77"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="20dp"
    android:layout_marginRight="35dp"
    android:background="@drawable/channel6" >
</ImageButton>
<!-- channel5 -->
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/ib_channel6"
    android:layout_marginBottom="20dp"
    android:layout_marginRight="10dp"
    android:layout_toLeftOf="@id/ib_channel6"
    android:background="@drawable/channel5" >
</ImageButton>
解决2:问题虽然解决了,但是却解决得并不完美。我们一般习惯按照布局的排版顺序依次列写我们的xml代码,但是像上述这样的xml并没有按照布局的顺序编写,而是为了解决找不到ID这个问题,把本应该写在前面的channel5和channel6后置了。总觉得很别扭,有没有什么方法既能解决找不到ID的问题,又能按照实际布局的顺序编写XML布局代码呢?答案是肯定的
操作:在values的目录,建立一个myids.xml文件,在里面提前声明id
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 手动声明资源ID,类似于C++中的前置声明 -->
    <item name="ib_channel6" type="id"></item>
    <item name="ib_channel7" type="id"></item>
</resources>  
然后就可以按照顺序来引用后面声明的id了