"No resource found that matches the given name" error even though the names match
Phan Van Linh's answer is correct, but here's why it's correct.
When you write @+id/foo
, you're saying "use the id foo
, and create it if it doesn't exist". When you write @id/foo
, you're saying "use the id foo
". If that id doesn't exist, this is an error (just like referencing a Java variable you haven't declared).
The reason it "works" if you put your layouts in the opposite order is that now your <Button android:id="@+id/foo">
tag comes first, so when the file is being processed, the id foo
is created before <ListView android:layout_above="@id/foo">
is processed.
In fact, if you swap the two views, build your project, and then swap them back to the original order, you'll find that it still works. This is because the generated R.java
file is built incrementally, so the id foo
sticks around from the last build.
But if you do a clean rebuild of your project, the problem will come back (because R.java is destroyed and regenerated).
I had a similar problem when I tried to run
<TextView android:id="@+id/text3" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Thanks for your friendship" android:fontFamily="casual" android:textColor="@android:color/white" android:textSize="27sp" android:layout_centerHorizontal="true" android:layout_above="@id/text4"/><TextView android:id="@+id/text4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Signed: Kingsley" android:fontFamily="casual" android:textColor="@android:color/white" android:textSize="36sp" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:paddingRight="8dp"/>
The solution was to simply reorder the text views.