【问题标题】:XML File Errors - no ressource foundXML 文件错误 - 未找到资源
【发布时间】:2012-11-27 17:57:31
【问题描述】:

这是我的问题。

我目前正在为井字游戏设置相对布局。 它由 9 个 ImageButtons 和 8 个用于获取网格的 Views 组成。

要么我的 xml 文件中有错误,说“找不到资源”(即使 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 文件中的“错误”,清理了项目,重新构建了它等等。没有任何效果。

【问题讨论】:

  • 究竟是什么错误?你能发布控制台构建信息吗?
  • 哪些资源找不到?一两个还是一个都没有?
  • '[2012-11-27 18:28:11 - TicTacToe] XML 文件中的错误:正在中止构建。 [2012-11-27 18:29:27 - TicTacToe] XML 文件中的错误:正在中止构建。而 r 类可用。尽管 id 已经在 r 类中,但错误都是“无法解析资源”。
  • 无法解析资源@id/hor2 无法解析资源@id/feld2 无法解析资源@id/vert1 无法解析资源@id/feld4 无法解析资源@ id/feld5 无法解析资源@id/vert2 无法解析资源@id/feld6 无法解析资源@id/hor4 无法解析资源@id/winners 是具有r 类时的错误。没关系,因为它们显然不是写在 r 类中的。所以我尝试清理项目和构建项目,因为如果 R 中缺少资源,我通常会这样修复它。但不是添加这个资源,R 消失了。

标签: android xml r.java-file


【解决方案1】:

虽然 Sam 走在正确的轨道上,但当您没有设置 android:id 属性时,用户 @+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/

【讨论】:

  • 感谢您的回答。我现在已经修好了。这只是 XML 文件中元素的顺序。如果您将视图 A 相对于另一个视图 B 对齐,则视图 B 必须在视图 A 的声明上方被赋予一个 Id。
  • @PaulLich 这如何回答您的问题?该示例在您的问题中产生 exact 错误:您在使用 @+id/winners 声明之前使用 @id/winners 引用获胜者...
  • @JamesMcCracken 无论在哪里@+id/ 代码引用了一个不存在的 id,您都会收到一个警告...并且订购视图的建议属于您自己的“不良做法”解释:如果视图 C 依赖于 B 依赖于 A 按照您描述的方式排序,删除视图 B 仍然需要您更新视图 C...
【解决方案2】:

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

<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"

您需要更新所有这些以使用正确的符号。

【讨论】:

  • 当你定义hor2时,你可以像这样定义它:android:id="@id/hor2,没有+
猜你喜欢
  • 1970-01-01
  • 2019-12-24
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多