Android xml 错误:“找不到与给定名称匹配的资源”与相对布局(@id/LinearLayout_acc、@id/ProgressBar_statusScreen)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11668718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:25:29  来源:igfitidea点击:

Android xml error: "No resource found that matches the given name" with RelativeLayout (@id/LinearLayout_acc, @id/ProgressBar_statusScreen)

androidxmleclipse

提问by stemadsen

Okay, so this is starting to really annoy me. This error pops up in a very special, not very logical way.

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

Let me start out by saying that I have already looked at the other questions regarding this error, Google'd it too. As far as I can tell, most similar problems occur because people refer to a Stringresource or something else not within the same layout file, they misplace the '+' in '@id+' or something similar.

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

The problem I am having occurs in a layout .xml file with a RelativeLayout. This contains a TableLayout, two LinearLayouts containing some text and finally a ProgressBar. What I want is the progress bar to be aligned with the relative layout using android:layout_alignParentBottom="true"and then align the two linear layouts above the progress bar (the bottom linear layout aligning above the progress bar, the other aligning above the bottom linear layout).

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

It should be simple enough and looks as if it works, i.e. the graphic view shows the desired result. However, and here comes the problem, Eclipse gives me an error on the two linear layouts,

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

"Error: No resource found that matches the given name (at 'layout_above' with value '@id/LinearLayout_acc')."

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

and the same error for the other linear layout referring to the progress bar. I have more than triple checked that there are no typos (the id's also exist in packagename.R.java), and I have tried cleaning the project a dozen times.

和其他线性布局参考进度条的相同错误。我已经多次检查没有错别字(ID 也存在于 packagename.R.java 中),并且我已经尝试清理该项目十几次。

I don't get the error when saving (and auto building), not until I decide to run the project. Another weird thing is that when I refer to the bottom linear layout from the progress bar instead of the top linear layout, I get no error!

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

My layout file:

我的布局文件:

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

Please help, I have no idea what causes this error!

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

Edit with answers

编辑答案

Shrikant came with the solution of changing the order of appearance in the layout file so that elements reference only other elements already defined when the reference is read.
Also, as others have posted, changing @id/to @+id/, even in a reference, does remove the error messages. As Marco W. wrote in thisthread, the thing is you have to use @+id/the first time each id is mentioned and then use @id/afterwards, even though the first time may not be a definition.

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

I made most of the design and set the referred id's in Eclipse's graphical editor, so code that resulted in an error message was automatically inserted. Maybe this is a bug in Eclipse.

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

采纳答案by Shrikant

Please check the below code

请检查以下代码

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

Also check the following link. It says that android:layout_below="@id/myTextView" won't recognise an element with id "myTextView" if it is written after the element you're using it in.

另请查看以下链接。它说 android:layout_below="@id/myTextView" 将无法识别 ID 为 "myTextView" 的元素,如果它写在您正在使用它的元素之后。

回答by jeet

change

改变

@id/LinearLayout_acc

to

@+id/LinearLayout_acc

回答by AkashG

Change every id @idto @+id, no matter when it's defining or referencing an id. With this, you won't get

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

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)。

回答by Hardik Nadiyapara

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