好的,所以这开始真的让我恼火。这个错误以一种非常特殊的方式弹出。
首先我要说的是,我已经查看了有关此错误的其他问题,谷歌也是这样。据我所知,大多数类似问题的出现是因为人们引用了String
资源或不在同一布局文件中的其他内容,他们错误地将'@ id +'中的'+'放在类似的东西中。
我遇到的问题出现在带有RelativeLayout
的布局.xml文件中。这包含TableLayout
,两个LinearLayout
包含一些文本,最后包含ProgressBar
。我想要的是使用android:layout_alignParentBottom="true"
将进度条与相对布局对齐,然后将两个线性布局对齐进度条(底部线性布局在进度条上方对齐,另一个在底部线性布局上方对齐) )。
它应该足够简单,看起来好像有效,即图形视图显示了所需的结果。但是,问题,Eclipse给出了两个线性布局的错误,
“错误:找不到与给定名称匹配的资源(在'layout_above'处,值为'@ id / LinearLayout_acc')。”
和参考进度条的其他线性布局的错误相同。我有三次以上检查没有拼写错误(包名也存在于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中的一个错误。
答案 0 :(得分:84)
变化
@id/LinearLayout_acc
到
@+id/LinearLayout_acc
答案 1 :(得分:76)
请检查以下代码
<?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”的元素,如果它是在你使用它的元素之后写的。
答案 2 :(得分:15)
将每个ID @id
更改为@+id
,无论它何时定义或引用ID。有了这个,你就不会得到
Android xml错误:“找不到与给定名称匹配的资源”和RelativeLayout(@ id / LinearLayout_acc,@ id / ProgressBar_statusScreen)。
答案 3 :(得分:2)
<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" >