I'm building a simple todo list app. I'm trying to organise my layout by positioning them relative to one another. I do that by calling XML attributes like android:layout_toStartOf="@id/to_do_btn"
but however I'm getting a No Resource Found That Matches The Given Name
error. I basically tried every solution on Stackoverflow but nothing helped. This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="myapp.onur.todo.MainActivity">
<ListView
android:id="@+id/to_do_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="@id/to_do_btn"
android:layout_margin="5dp"
>
</ListView>
<EditText
android:id="@+id/to_do_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Something To Do"
android:layout_toStartOf="@id/to_do_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@id/to_do_btn"
android:layout_below="@id/to_do_list"
android:layout_margin="5dp"
/>
<Button
android:id="@+id/to_do_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add item"
android:layout_margin="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
And this is an example of the error: Error:(15, 31) No resource found that matches the given name (at 'layout_above' with value '@id/to_do_btn').
I have no idea what's going on. If it helps, here are my gradle dependencies:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
}
Change
android:layout_above="@id/to_do_btn"
to
android:layout_above="@+id/to_do_btn"
OR order your layout like this
<Button
android:id="@+id/to_do_btn"
...
/>
<ListView
...
android:layout_above="@id/to_do_btn"
>
</ListView>
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With