好吧,这开始让我很恼火了。这个错误以一种非常特殊、不太合乎逻辑的方式出现。
让我开始说,我已经研究了关于这个错误的其他问题,谷歌也解决了它。据我所知,大多数类似的问题发生是因为人们引用了String
资源或其他不在同一布局文件中的东西,他们在'@id+‘或类似的东西中放错了'+’。
我遇到的问题发生在带有RelativeLayout
的布局.xml文件中。它包含一个TableLayout
,两个包含一些文本的LinearLayout
,最后是一个ProgressBar
。我想要的是使用android:layout_alignParentBottom="true"
将进度条与相对布局对齐,然后将进度条上方的两个线性布局对齐(底部线性布局在进度条上方对齐,另一个在底部线性布局上方对齐)。
它应该足够简单,而且看起来像是在工作,也就是说,图形视图显示了所需的结果。然而,出现了问题,Eclipse在两个线性布局上给了我一个错误,
“错误:找不到与给定名称匹配的资源(位于值为‘@id/LinearLayout_acc’的'layout_above‘)。”
并且对于涉及进度条的另一个线性布局也有相同的错误。我已经检查了三次以上,没有输入错误(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中的一个bug。
发布于 2012-07-26 20:41:57
请检查以下代码
<?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。它指出如果一个id为"myTextView“的元素被写在你正在使用的元素之后,android:layout_below="@id/myTextView”将无法识别该元素。
发布于 2012-07-26 19:56:55
变化
@id/LinearLayout_acc
至
@+id/LinearLayout_acc
发布于 2012-07-26 19:58:58
将每个id @id
更改为@+id
,无论何时定义或引用id。有了这个,你就不会得到
Android xml错误:使用RelativeLayout (@id/LinearLayout_acc,@id/ProgressBar_statusScreen).
)找不到与给定名称匹配的资源
https://stackoverflow.com/questions/11668718
复制相似问题