Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No resource found that matches the given name" error even though the names match

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'
}
like image 570
oo92 Avatar asked Jul 26 '17 03:07

oo92


2 Answers

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>
like image 110
Linh Avatar answered Nov 11 '22 06:11

Linh


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

like image 39
Ben P. Avatar answered Nov 11 '22 06:11

Ben P.