XML文件错误 - 找不到资源

时间:2012-11-27 17:57:31

标签: android xml r.java-file

这是我的问题。

我目前正在为TicTacToe-Game设置相对布局。 它由9个ImageButtons和8个视图组成,我用它来获取网格。

我的xmlfile中有错误,说“找不到资源”(即使id正确地构建到我的r.class中),或者当我重新键入布局引用到其他视图时错误消失。但是,如果我重新键入它,清理项目并再次构建它,eclipse让我的R.class消失。

这是我的XML文件:

    `<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/notepad2" >    

    <View 
    android:layout_width="5dp" 
    android:layout_height="160dp"
    android:background="#FF0000"
    android:layout_toLeftOf="@id/hor2"
    android:id="@+id/vert1"
    />

 <View 
    android:layout_width="5dp" 
    android:layout_height="160dp"
    android:background="#FF0000"
    android:layout_toLeftOf="@id/hor3"
    android:id="@+id/vert2"
    />

 <View 
    android:layout_width="50dp" 
    android:layout_height="5dp"
    android:background="#FF0000"

    android:layout_alignLeft="@id/feld4"
    android:layout_above="@id/feld4"
    android:id="@+id/hor1"
    />

 <View 
    android:layout_width="50dp" 
    android:layout_height="5dp"
    android:background="#FF0000"

    android:layout_alignLeft="@id/feld5"
    android:layout_above="@id/feld5"
    android:id="@+id/hor2"
    />



 <View 
    android:layout_width="50dp" 
    android:layout_height="5dp"
    android:background="#FF0000"

    android:layout_alignLeft="@id/feld6"
    android:layout_above="@id/feld6"
    android:id="@+id/hor3"
    />

  <View 
    android:layout_width="50dp" 
    android:layout_height="5dp"
    android:background="#FF0000"

    android:layout_alignLeft="@id/feld4"
    android:layout_below="@id/feld4"
    android:id="@+id/hor4"
    />

 <View 
    android:layout_width="50dp" 
    android:layout_height="5dp"
    android:background="#FF0000"

    android:layout_alignLeft="@id/feld5"
    android:layout_below="@id/feld5"
    android:id="@+id/hor5"
    />

 <View 
    android:layout_width="50dp" 
    android:layout_height="5dp"
    android:background="#FF0000"

    android:layout_alignLeft="@id/feld5"
    android:layout_below="@id/feld6"
    android:id="@+id/hor6"
    />




<ImageButton
    android:id="@+id/feld1"
    android:layout_width="50dp"
    android:layout_height="50dp"

    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_toLeftOf="@id/vert1"
    android:layout_alignTop="@id/vert1"
    />

<ImageButton
    android:id="@+id/feld2"
    android:layout_width="50dp"
    android:layout_height="50dp"

    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_above="@id/hor2"
    android:layout_alignLeft="@id/hor2"
     />

 <ImageButton
    android:id="@+id/feld3"
    android:layout_width="50dp"
    android:layout_height="50dp"

    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_toRightOf="@id/vert2"
    android:layout_alignTop="@id/vert2"
    />

  <ImageButton
    android:id="@+id/feld4"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_toLeftOf="@id/vert1"
    android:layout_alignTop="@id/feld5"
    />

 <ImageButton
    android:id="@+id/feld5"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_centerInParent="true"
    />

<ImageButton
    android:id="@+id/feld6"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_toRightOf="@id/vert2"
    android:layout_alignTop="@id/feld5"
    />

<ImageButton
    android:id="@+id/feld7"
    android:layout_width="50dp"
    android:layout_height="50dp"

    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_toLeftOf="@id/vert1"
    android:layout_below="@id/hor4"
    />

<ImageButton
    android:id="@+id/feld8"
    android:layout_width="50dp"
    android:layout_height="50dp"

    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_toRightOf="@id/vert1"
    android:layout_below="@id/hor4"
    />

<ImageButton
    android:id="@+id/feld9"
    android:layout_width="50dp"
    android:layout_height="50dp"

    android:background="@drawable/leer"
    android:contentDescription="@string/feld"
    android:layout_toRightOf="@id/vert2"
    android:layout_below="@id/hor4"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/winners"
    android:text="@string/winner"
    android:layout_alignParentBottom="true"
    android:textColor="#FFFFFF"
/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/anotherGame"
    android:visibility="invisible"
    android:layout_above="@id/winners"
    android:text="@string/newGameButton"
/>

`

任何人都知道如何修复?

我已经尝试了平时。更正了xml文件中的“错误”,清理了项目,再次构建了它等等。没有任何效果。

2 个答案:

答案 0 :(得分:0)

我从你的第一个视图中看到了很多像这样的问题:

<View 
    android:layout_width="5dp" 
    android:layout_height="160dp"
    android:background="#FF0000"
    android:layout_toLeftOf="@id/hor2"
    android:id="@+id/vert1"
    />

hor2尚未宣布,您必须使用@+id

    android:layout_toLeftOf="@+id/hor2"

您需要更新所有这些以使用正确的表示法。

答案 1 :(得分:0)

当Sam走在正确的轨道上时,如果您未设置@+id/属性,则用户android:id实际上是不好的做法。 @+id/实际上会尝试将该ID添加到您的R文件中(如果它尚不存在)。这可能会导致您删除另一个基于的元素但由于您使用@+id/

而不会抛出错误的问题

相反,您应该在xml文件中定义基于视图对齐的视图上方的布局。例如,winners应低于anotherGame

<Button
    android:id="@+id/anotherGame"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/winners"
    android:text="@string/newGameButton"
    android:visibility="invisible" />

<TextView
    android:id="@+id/winners"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/winner"
    android:textColor="#FFFFFF" />

如果你绝对无法绕过基于它们的视图上方声明的所有视图,那么你可以使用@+id/