This is driving me mad. My project compiled fine a moment ago. I made some minor change somewhere else, and now I'm getting this error. Here's the error message in full:
no resource found that matches the given name (at 'layout_above' with value '#id/blank_view").
Here's my XML file where the error is occurring:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_right"
android:layout_above="#id/blank_view"
android:src="#drawable/button" />
<View
android:id="#+id/blank_view"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_bottom"
android:layout_alignParentBottom="true" />
</RelativeLayout>
How in the hell could it be failing to find a resource with the name "#id/blank_view" when that resource is just below it in the file?
Btw, the reason I have my layout like this is that I want the ImageButton to be aligned to the bottom of my relative layout, but offset up by a margin. For some reason, those two attributes (layout_alignParentBottom and layout_marginBottom) don't mix well in the same view.
I should also point out that this happened earlier as well, but I just removed the reference that was giving AndroidStudio such a problem, rather than trying to fix it. This, however, is too important to wave away like that.
This happens because the xml is parsed in a linear fashion and the views/objects created in a top to bottom order. So your xml tells the view builder to put your ImageButton above an item that does not exist yet.
Just move it below the blank view and the error should go away.
Try to add the property to your view:
<view
android:id="#+id/blank_view"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_bottom"
android:layout_alignParentBottom="true"
android:layout_below="#id/btn"
/>
And remove:
android:layout_above="#id/blank_view"
Related
How do I reference a later XML element?
Here's a specific use case. Let's say I have a form with a root LinearLayout, containing LinearLayouts for multiple rows, each row having one or more text input areas.
Here's a visual of what I'm going for. First pic is from Venmo's app, second is a rendering of the following XML.
Such a layout could look like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/row_card_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/card_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nextFocusDown="#id/month"/>
</LinearLayout>
<LinearLayout
android:id="#+id/row_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/month"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:nextFocusDown="#id/year"/>
<EditText
android:id="#+id/year"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
</LinearLayout>
In this use case, forward referencing is necassary in order to set the next focus element. That way, when you press the next button on the keyboard, it'll go to the correct view. In this sample xml, without the nextFocusDowns, pressing next would go from name to month, and never go to year.
However, if you try to compile this, you'll get an error:
Error:(18, 36) No resource found that matches the given name (at 'nextFocusDown' with value '#id/month').
This is because the id month hasn't yet been initialized when I'm trying to reference it, since that's later in the file. How can I reference an id in xml that appears later in the file?
The simplest solution is just to replace
android:nextFocusDown="#id/month"
with
android:nextFocusDown="#+id/month"
When the compiler is parsing your XML to add the id's to R.java, it just reads top to bottom. When you have #id/month, it searches through the existing id's, and fails to find it.
However, if you do #+id/month, it creates a new id, and links to that. When it gets to android:id=#+id/month in the actual month view, it links it to the same id that we already created.
This brings up the question: If you can replace #id/ with #+id/, and #+id/ will work regardless of the order of elements, why even bother to use #id/?
The reason for this is if the id doesn't exist, #id/ will throw a compiler error, while #+id/ will log a warning at runtime.
Consider this XML:
<EditText
android:id="#+id/month"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:nextFocusDown="#+id/SOME_RANDOM_ID"/>
<EditText
android:id="#+id/year"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
When this is parsed, a new id element SOME_RANDOM_ID is created. However, when Android tries to apply it at runtime, it can't find an element with that id. If you look at Logcat, you'll see this:
W/View﹕ couldn't find view with id 2131689604
This log message is both hard to find and hard to debug. One small typo in a #+id/ and you'll have a bug that could be incredibly difficult to debug. However, if we had done:
android:nextFocusDown="#id/SOME_RANDOM_ID"
Then we'd get a compiler error, something like:
Error:(18, 36) No resource found that matches the given name (at 'nextFocusDown' with value '#id/SOME_RANDOM_ID').
This is much easier to find and debug.
tl;dr: You can use #+id/ instead of #id/ and you'll be able to forward reference, but note that that can make small typos incredibly difficult to debug.
You might be able to use a RelativeLayout to make all the Views exist in reverse order in the xml, but that seems like overkill to me.
I had the same issue recently and I used #+id/my_new_id the first time I referenced the element and later in the XML in the element definition, I assigned #id/my_new_id to the android:id attribute. It seems it works fine and it's not necessary write #+id with the same id more than one time avoiding possible warnings.
For example:
<LinearLayout
...
android:layout_toLeftOf="#+id/my_new_id"
... >
...
</LinearLayout>
<ImageButton
android:id="#id/my_new_id"
... />
I've used toLeftOf many times before but it suddenly stopped working. I tried:
android:layout_toLeftOf="#id/spacer"
Here is the view:
<View
android:id="#+id/spacer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/center_margin"
android:layout_marginStart="#dimen/center_margin"
android:layout_marginRight="#dimen/center_margin"
android:layout_marginEnd="#dimen/center_margin" />
And it gave me the errors:
Error:(16, 34) No resource found that matches the given name (at 'layout_toLeftOf' with value '#id/spacer').
Error:(17, 35) No resource found that matches the given name (at 'layout_toStartOf' with value '#id/spacer').
It works at other points in the app but for some reason it doesn't work here. Anyone know whats wrong?
Edit - Yes it is in a RelativeLayout. Here is a condensed version:
<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"
android:screenOrientation="portrait"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true" >
<TextView
android:id="#+id/txt1" />
<View
android:id="#+id/spacer" />
<TextView
android:id="#+id/txt2" />
</RelativeLayout>
Upon further review I figured out the issue is I was trying to reference the View before it was made. I fixed this by changing #id/spacer to #+id/spacer
A bit late, but maybe still helpful for others. Took me 1 hour to find out my issue.
Non working example:
<TextView
android:id="#+id/firstview"
android:layout_toRightOf="#id/secondview"/>
<TextView
android:id="#+id/secondview"/>
Working example
<TextView
android:id="#+id/firstview"
android:layout_toRightOf="#+id/secondview"/>
<TextView
android:id="#+id/secondview"/>
Not the sign "+" being the only difference.
If you reference a view which is defined after the view you put the reference in, you need to use a +
Write the layout which is used as refrence, then the layout you want to put relative to that layout...
It's like trying to use variable before it was declared in the same class file.
You cannot reference a property that is not found in your respective ViewGroup. In this case you are trying to access properties that are specific to RelativeLayout. You cannot access these kinds of properties UNLESS your encapsulating view is in fact a RelativeLayout.
Note that this applies to the Parent layout that the view is encapsulated in.
I have two textviews declared as below. My intention is to have two views side by side, where left view (displaying user name) has bigger text size, right view displays time of last message send and should be always visible (even if user has really long name, that's why I'm using android:layout_toLeftOf). However, left view is smaller and I want to align its baseline to right view. It's really nice dependency where and I'm not able to solve it.
Partially acceptable solution is to use "android:layout_toRightOf" in right view, but if user has really long name, then time (right view) will be ellipsized (it's declared in AppTheme.TextView.SingleLine).
So basically, my questions is, is it possible for two views to reference each other? I understand why I'm getting this error, but I'm not able to solve it.
I remember from my C/C++ times that it was possible to declare function in the top of the file and then define it somewhere else (so the compiler doesn't complain) and I think it's something what I need here.
<TextView
android:id="#+id/fragment_messages_item_sender_name"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/fragment_messages_item_last_msg_time"
android:textSize="#dimen/global_text_large"/>
<TextView
android:id="#+id/fragment_messages_item_last_msg_time"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/fragment_messages_item_sender_name"
android:layout_alignParentEnd="true"
android:gravity="right"/>
All I'm getting is
Error:(27, 38) No resource found that matches the given name (at 'layout_toLeftOf' with value '#id/fragment_messages_item_last_msg_time').
in R class id like
fragment_messages_item_last_msg_time
not exist
for avoid that problem need to use "+" before declaration field with id relation
android:layout_toLeftOf="+#id/fragment_messages_item_last_msg_time"
you could move the android:layout_alignBaseline in the other TextView. Be aware of id loops, that usually generated nasty crashes.
About your issue, you have to remember the entries inside R are marked as public static final, and that the + generates a new entry for the specify id, if it does not exists, So you can have:
android:layout_toLeftOf="#+id/fragment_messages_item_last_msg_time"
in the first TextView, and
android:id="#id/fragment_messages_item_last_msg_time"
to the second one. As I mentioned before, loops in RelativeLayout are not allowed, and those will make your app crash
Hi use below code :
<LinearLayout 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"
android:orientation="horizontal" >
<TextView
android:id="#+id/fragment_messages_item_sender_name"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manish" />
<TextView
android:id="#+id/fragment_messages_item_last_msg_time"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Khamar" />
</LinearLayout>
Im trying to relate widgets , I write the code as bellow:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/ad1"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/ad1"
android:orientation="horizontal"
android:layout_toStartOf="#id/bt_menu" > // here there is error !!!!
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="Menu "
android:inputType="textVisiblePassword"/>
</RelativeLayout>
<ImageButton
android:id="#+id/bt_menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/menu5"
android:onClick="menu_onclick"/>
</RelativeLayout>
I have an error in this line:
android:layout_toStartOf="#id/bt_menu"
The error is : no resource found that matches the given name (at 'layout_toStartOf' with value '#id/bt_menu')
Can anyone help me please !!!!
Thank you in advance .. Fadel.
Change it to
android:layout_toStartOf="#+id/bt_menu" >
Add the "+"
If it is before the layout you are referencing then Eclipse doesn't seem to know about it until it has been saved. You should be able to change it back to how you have it after you have run it once. But I don't know that it will hurt anything to leave it. I do this if I use a property such as layout_below but put it before the View I want it to be relative to or something similar but I just leave it.
There may be a better way to handle this witin Eclipse, or whatever editor you are using, but AFAIK, this is the simplest and I don't know of any undesirable effects from it.
In my opition, is not the best way to use "#+id/" twice. You can receive errors of your R.java file. The problem you got is, that you use the id before you set it.
Solution:
Define id first by
android:layout_toStartOf="#+id/bt_menu"
Use id
android:id="#id/bt_menu"
I am creating a button in my XML and here is the creation parameters
<Button android:id="#+id/btn2"
---> android:layout_width="wrap_content" <----
android:layout_height="wrap_content"
android:text="#string/PNR"
/>
I am getting and error in the line indicated saying :
" Element type "Button" must be followed by either attribute specifications, ">" or "/>" "
Not only in button id I try to create TextView or so then also same error comes and at same place.
I have checked earlier posts but they said that the tags were not closed and did not worked for me.
Please suggest me, what to do? Here is the full code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/PNR"
/>
</LinearLayout>
Try cleaning your project
Project --> Clean... then choose your project
Sometimes Eclipse doesn't pick up changes to your xml. When you get goofy errors like this always try cleaning first. Sometimes you will get ClassCastException in Java code when running right after changing something in xml like
cannot cast Button to EditText
or something similar that won't make sense. This is also a good time to clean your project.
I would also recommend getting rid of whitespace within elements because I have had trouble with that as well (especially on older versions of Eclipse) plus I think it looks cleaner. So I would change your <Button... to
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/PNR"/> <!-- just moved your end tag to this line -->