【问题标题】:Android xml error: "No resource found that matches the given name" with RelativeLayout (@id/LinearLayout_acc, @id/ProgressBar_statusScreen)Android xml 错误:“未找到与给定名称匹配的资源”与 RelativeLayout(@id/LinearLayout_acc,@id/ProgressBar_statusScreen)
【发布时间】:2012-07-25 00:07:50
【问题描述】:

好的,所以这开始让我很恼火。这个错误以一种非常特殊、不太合乎逻辑的方式弹出。

首先让我说我已经查看了有关此错误的其他问题,谷歌也找到了。据我所知,大多数类似问题的发生是因为人们引用了String 资源或不在同一个布局文件中的其他内容,他们在“@id+”或类似内容中放错了“+”。

我遇到的问题出现在带有RelativeLayout 的布局.xml 文件中。这包含一个TableLayout,两个包含一些文本的LinearLayouts,最后是一个ProgressBar。我想要的是使用android:layout_alignParentBottom="true" 将进度条与相对布局对齐,然后在进度条上方对齐两个线性布局(底部线性布局在进度条上方对齐,另一个在底部线性布局上方对齐)。

它应该足够简单并且看起来好像可以工作,即图形视图显示了所需的结果。然而,问题来了,Eclipse 在两个线性布局上给了我一个错误,

“错误:未找到与给定名称匹配的资源(在 'layout_above' 处,值为 '@id/LinearLayout_acc')。”

对于其他涉及进度条的线性布局也有同样的错误。我已经检查了三次以上,没有错别字(ID 也存在于 packagename.R.java 中),并且我已经尝试清理项目十几次。

保存(和自动构建)时我没有收到错误,直到我决定运行项目。另一个奇怪的事情是,当我从进度条而不是顶部线性布局引用底部线性布局时,我没有收到错误!

我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_activity" >
        <TableLayout
             ... />

        <LinearLayout
            android:id="@+id/LinearLayout_dist"
            android:layout_above="@id/LinearLayout_acc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp" >

            <TextView
                ... />

            <TextView
                ... />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/LinearLayout_acc"
            android:layout_above="@id/ProgressBar_statusScreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" >

            <TextView
                ... />

            <TextView
                ... />
        </LinearLayout>

        <ProgressBar
            android:id="@+id/ProgressBar_statusScreen"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_margin="16dp" />

</RelativeLayout>

请帮忙,我不知道是什么导致了这个错误!

用答案编辑

Shrikant 提供了更改布局文件中出现顺序的解决方案,以便元素仅引用在读取引用时已定义的其他元素。
此外,正如其他人发布的那样,将@id/ 更改为@+id/,即使在参考中,也会删除错误消息。正如 Marco W. 在this 线程中所写,问题是您必须在第一次提到每个 id 时使用@+id/,然后再使用@id/,即使第一次可能不是一个定义。

我完成了大部分设计并在 Eclipse 的图形编辑器中设置了引用的 id,因此会自动插入导致错误消息的代码。也许这是 Eclipse 中的一个错误。

【问题讨论】:

    标签: android xml eclipse


    【解决方案1】:

    改变

    @id/LinearLayout_acc
    

    @+id/LinearLayout_acc
    

    【讨论】:

    • 哇,这确实有效。我认为@id/ 用于引用现有 ID,@+id/ 用于创建新 ID,并且这些 不能 可以互换。这种方法怎么合法?那么我定义了两次id呢?
    • 其实id是long值,@+id/指的是long值。如果我们为某个组件分配了一些 id 值,那么我们只能使用这个 id 来引用视图。
    • 这听起来很奇怪,因为在所有其他布局中,我使用@id/ 而不是@+id/ 引用其他ID。此外,正如 Shrikant 下面指出的,如果我更改引用元素的顺序,它可以与 @id/ 一起使用。
    • 非常好的方法可以让元素按照它们在屏幕上出现的顺序保持在相对布局中,这正是我想要的。
    • 是的,先生!给那个人一枚勋章。如果需要引用稍后在布局中创建的视图,这正是您必须做的。
    【解决方案2】:

    请检查以下代码

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/ic_launcher" >
    
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <LinearLayout
        android:id="@+id/LinearLayout_dist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/LinearLayout_acc"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FIRST" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SECOND" />
       </LinearLayout>
    
       <LinearLayout
        android:id="@+id/LinearLayout_acc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ProgressBar_statusScreen"
        android:layout_centerHorizontal="true" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="THIRD" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FOURTH" />
       </LinearLayout>
    
       <ProgressBar
        android:id="@+id/ProgressBar_statusScreen"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="16dp" />
    
     </RelativeLayout>
    

    同时检查以下link。 它表示 android:layout_below="@id/myTextView" 将无法识别 ID 为“myTextView”的元素,如果它写在您使用它的元素之后。

    【讨论】:

    • 感谢您的评论:android:layout_below="@id/myTextView" 将无法识别 ID 为“myTextView”的元素,如果它写在您使用它的元素之后。这解决了问题,尽管我认为您必须以“正确”顺序编写元素是荒谬的......可能是一个错误。
    • 确定这是一个错误。布局 ID 应该独立于定义顺序。尤其烦人的是,Android Studio 中的屏幕布局设计器一点问题都没有。
    • @stemadsen 这不是错误。解析器仅在第一次遇到视图元素时创建视图和它的引用 (Id)。您正在引用未访问的元素。考虑一个场景,A在B下面定义自己,然后B在A下面定义自己。排序的限制解决了逻辑问题
    • 让我在这里说明一下:对于您正在处理的 UI 元素,在它之后(而不是之前)编写的 UI 元素是不可能引用的。
    【解决方案3】:

    将每个 id @id 更改为 @+id,无论何时定义或引用一个 id。有了这个,你不会得到

    Android xml 错误:“未找到与给定名称匹配的资源”与 RelativeLayout(@id/LinearLayout_acc、@id/ProgressBar_statusScreen)。

    【讨论】:

      【解决方案4】:
       <LinearLayout
              android:id="@+id/LinearLayout_dist"
              android:layout_above="@+id/LinearLayout_acc" <--- here might be a problem you forgot + sign
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_marginBottom="10dp" >
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-24
        • 1970-01-01
        • 2016-09-18
        • 1970-01-01
        相关资源
        最近更新 更多