92

Okay, so this is starting to really annoy me. This error pops up in a very special, not very logical way.

Let me start out by saying that I have already looked at the other questions regarding this error, Google'd it too. As far as I can tell, most similar problems occur because people refer to a String resource or something else not within the same layout file, they misplace the '+' in '@id+' or something similar.

The problem I am having occurs in a layout .xml file with a RelativeLayout. This contains a TableLayout, two LinearLayouts containing some text and finally a ProgressBar. What I want is the progress bar to be aligned with the relative layout using android:layout_alignParentBottom="true" and then align the two linear layouts above the progress bar (the bottom linear layout aligning above the progress bar, the other aligning above the bottom linear layout).

It should be simple enough and looks as if it works, i.e. the graphic view shows the desired result. However, and here comes the problem, Eclipse gives me an error on the two linear layouts,

"Error: No resource found that matches the given name (at 'layout_above' with value '@id/LinearLayout_acc')."

and the same error for the other linear layout referring to the progress bar. I have more than triple checked that there are no typos (the id's also exist in packagename.R.java), and I have tried cleaning the project a dozen times.

I don't get the error when saving (and auto building), not until I decide to run the project. Another weird thing is that when I refer to the bottom linear layout from the progress bar instead of the top linear layout, I get no error!

My layout file:

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

Please help, I have no idea what causes this error!

Edit with answers

Shrikant came with the solution of changing the order of appearance in the layout file so that elements reference only other elements already defined when the reference is read.
Also, as others have posted, changing @id/ to @+id/, even in a reference, does remove the error messages. As Marco W. wrote in this thread, the thing is you have to use @+id/ the first time each id is mentioned and then use @id/ afterwards, even though the first time may not be a definition.

I made most of the design and set the referred id's in Eclipse's graphical editor, so code that resulted in an error message was automatically inserted. Maybe this is a bug in Eclipse.

Community
  • 1
  • 1
stemadsen
  • 1,841
  • 2
  • 16
  • 15

4 Answers4

84

change

@id/LinearLayout_acc

to

@+id/LinearLayout_acc
jeet
  • 29,001
  • 6
  • 52
  • 53
  • 3
    Wow, this actually works. I thought `@id/` was for referencing existing id's, `@+id/` for creating new id's and that these could _not_ be interchanged. How come this approach is legal? I define the id twice then? – stemadsen Jul 26 '12 at 19:05
  • actually, its a long value for id, @+id/ refers to some long value. and if we assigned some id value to some component, then we can use only this id to reference the views. – jeet Jul 27 '12 at 04:31
  • This sounds strange, because in all other layouts I reference other id's with @id/ and not @+id/. Also, as Shrikant noted below, if I change the order of the referenced elements, it works with @id/. – stemadsen Jul 28 '12 at 14:22
  • 1
    Really nice way to keep elements in a Relative Layout in the order they will appear on screen, exactly what I was looking for. – GLee Jul 15 '15 at 18:57
  • 2
    YES SIR! Give that man a medal. This is exactly what you have to do, if it is needed to reference a View that is created later in layout. – Yoraco Gonzales Jan 14 '17 at 14:05
77

Please check the below code

<?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>

Also check the following link. It says that android:layout_below="@id/myTextView" won't recognise an element with id "myTextView" if it is written after the element you're using it in.

Community
  • 1
  • 1
Shrikant
  • 1,560
  • 1
  • 15
  • 32
  • 8
    Thanks for this comment: _android:layout_below="@id/myTextView" won't recognise an element with id "myTextView" if it is written after the element you're using it in_. This fixed the problem, even though I think it's ridiculous that you have to write the elements in the "correct" order ... Possibly a bug. – stemadsen Jul 26 '12 at 18:58
  • 4
    For sure this is a bug. The layout IDs should be independent of definition order. Especially annoying that the onscreen layout designer in Android Studio has no trouble with it. – dodgy_coder Nov 24 '15 at 03:14
  • @stemadsen it is not a bug. parser simply creates the view and it's refrences (Id) first time it meets the view element. you are referencing a non visited element. consider a scenario which A defines itself below B and later B defines itself below A. the limitation of ordering solves the logical problem – Masoud Dadashi Jun 15 '16 at 22:23
  • Let me remark here: it's not possible to make references on the UI element which you are working on for UI elements written AFTER it (not before). – ivanleoncz Nov 12 '17 at 04:51
14

Change every id @id to @+id, no matter when it's defining or referencing an id. With this, you won't get

Android xml error: “No resource found that matches the given name” with RelativeLayout (@id/LinearLayout_acc, @id/ProgressBar_statusScreen).

Pang
  • 9,564
  • 146
  • 81
  • 122
AkashG
  • 7,868
  • 3
  • 28
  • 43
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" >
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24