No resource found error in layout xml file android - android

I get this error in xml file very often.
here is the code in xml file
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_above="#id/tRowMain" // in this line i get error resource not found that matches given name
android:textColor="#color/selectLevel"
android:id="#+id/tvOnOption"
android:text="Select Mode"
/>
<TableRow android:layout_width="fill_parent" android:id="#+id/tRowMain"
android:layout_height="wrap_content" android:gravity="center" android:layout_centerVertical="true" android:layout_centerHorizontal="true" >
//then i checked in R.java file and the id for this name is there
public static final class id {
public static final int ibtn_retry=0x7f060006;
public static final int rLayoutMain=0x7f060000;
public static final int tRowMain=0x7f060002;
}
please help me figure out whats wrong with this...
thanks

You should use the #+id/tRowMain syntax in the first place the ID is used, not necessarily the first place where you define it as the ID of the element.
Change:
android:layout_above="#id/tRowMain" to android:layout_above="#+id/tRowMain"
and
android:id="#+id/tRowMain" to android:id="#id/tRowMain
In other words, when deciding whether or not to use #+id or #id, it doesn't matter which attribute you're assigning the id to. Always use #+id the first time you mention your ID in the XML.

android:layout_above="#+id/tRowMain"
If it does'nt work delete your R.java file. It will be re-generated

Related

Where do I find the parameter to pass into the findViewById in Android development

I have just started learning app development and I am running into the problem of not knowing what I should give the findViewById function nor where I can find it.
In your XML layout, each View has an id. You use this id in your findViewById method.
Example:
<TextView
android:id="#+id/my_textview"
android:text="Hello world" />
And the corresponding code in Java to reference this TextView:
TextView tv = (TextView) findViewById(R.id.my_textview);
Give it a resource id. findViewById is a method of View class object.
You can obtain the official reference at:
http://developer.android.com/reference/android/app/Activity.html#findViewById(int)
To start your learning:
http://developer.android.com/guide/index.html
findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
The easiest way to find element of the Android app layout is to use its ID. We could add our own IDs to almost every XML tags. To add ID we use android:id attribute.
<TextView
android:id="#+id/this_is_id_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_best_text" />
We have to set in brackets a type of View we’re looking for and then give type and name of resources: (view_type) findViewById(R.id.id_name).
TextView newtext = (TextView) findViewById(R.id.this_is_id_name);
For more information you may visit Here.

set textsize for multiple textview with same ID

Is it possible to set textsize for multiple textview with same ID ? I have them with android:id :"#+id/lbl" ? Actually I set dp based on folder values/dimen.xml
TextView lbl=(TextView) findViewById(R.id.lbl);
lbl.setTextSize(dp);
The first textview only get this effect, but the rest of textview dont get this effect. Let me know how to solve this problem.
You need to do it separately as every activty/fragment/adapter has its own layouts (xml). In R.java they would have its own unique number.
android:textSize="24sp"
Everytime you add the views in XML, just add the text size
In android platform each view must have a unique id identified by "android:id" attribute of view. Although you are able to define multiple views with same id but android very first declaration and leaves the rest view as having no id.
Lets have an example now:
<TextView
android:id="#+id/response_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="27dp"
android:layout_toLeftOf="#+id/scan_format"
android:text="TextView" />
<TextView
android:id="#+id/response_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/response_text"
android:layout_marginBottom="74dp"
android:layout_toLeftOf="#+id/response_text"
android:text="Trepeattet" />
here are two textviews with same id, and now see R.java
public static final int radio=0x7f05002f;
public static final int response_text=0x7f050040;
public static final int scan_button=0x7f05003c;
public static final int scan_content=0x7f05003e;
public static final int scan_format=0x7f05003d;
in R.java only one textview is declared with its id(this hex id is created by platform compiler itself when you first run your code).
So although application does not produce any error in compile time or run time but will not do anything to any other view with same id either except very first one because of only one reference available that belongs to first textview.
As per this post here is what you can do:
View IDs are plain integers, so you can simply have an array of ids you want to change. For example:
int[] ids = { R.id.lbl1, R.id.lbl2, ... };
for (int id: ids) {
TextView lbl=(TextView) findViewById(id);
lbl.setTextSize(dp);
}
This will work.
You can get all TextViews of your parent view. Then check their ids in a loop

Dynamically change the name of the resource file to be used?

It is possible to do something like:
if (colorScheme == 1)
button.setBackgroundResource(R.drawable.button + "_1")
in order to use R.drawable.button_1 as the resource for this button in color scheme 1, if there are files named button_1.png, button_2.png, button_3.png in drawable folder.
(dynamically use different resource file for the same UI element based on the color scheme being used?)
Thanks,
I've done something simular using getIdentifier():
int resId = context.getResources().getIdentifier("button_1","drawable",context.getPackageName());
button.setBackgroundResource(resId);
In order for it to be dynamic, there will be some code required. You can set up your layout in xml Like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
And then reference it in your code like this:
int resId = context.getResources().getIdentifier("button_1","drawable",context.getPackageName());
Button button = (Button view.findViewById(R.id.button1);
button.setBackgroundResource(resId);
I haven't tested this, but this should give you the idea.
Put R.drawable.button_n in an array int res[] and then call them by button.setBackgroundResource(res[i])

Android find resource by id during the runtime

If I get the error "android.content.res.Resources$NotFoundException: Resource ID #0x7f050007 type #0x12 is not valid" can I find some what this resource is if I know its ID?
ListView list = (ListView)findViewById(R.id.messages_list_view);
list.setAdapter(new ArrayAdapter<String>(context,
R.layout.messages_list, headers));
messages_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/messages_list_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="#+id/messages_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I Got this error when using ListView in a Fragment.
Resolved by moving the setAdapter lines to the onViewCreated function of the Fragment. (makes sense that before the view is created the ListView is invalid).
so you get :
public void onViewCreated(View view,Bundle savedInstanceState){
ListView list = (ListView) getView().findViewById(R.id.thelist);
list.setAdapter(mAdapter);
}
For those whom other mentioned solutions don't work.
I did this silly mistake:-
setContentView(R.id.something);
Instead of
setContentView(R.layout.something);
Corrected that, and error was gone :D
You can either use the search function in eclipse, search for "0x7f050007" or go to projectfolder/gen/path/R.java that contains your resources.
You'll find something like this:
public static final int lineItem=0x7f07001c;
Then search for(in this example) lineItem with Eclipses search function. It'll take you to your resource in code.
Check your imports (at the top of your class-file). Maybe you imported
android.R
(which provides access to the platform-resources) instead of
{your_package_name}.R
(you can also leave it blank).

textView.setText(); crashes

The setText() method returns null in my application why?
public class GetValue extends Activity {
char letter = 'g';
int ascii = letter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView)findViewById(R.id.txt_1);
textView.setText(ascii);
}
}
It doesn't matter what text i put in, it crashes anyway. Why does setText() keep returning null?
Thank you, in advance
Solution: My error was in the xml file. I wrote:
android:text="#+id/txt_1"
When it should say:
android:id="#+id/txt_1"
Thanks a lot for all the answers and comments!
You tried to pass an integer as parameter to setText, which assumes it is a resource ID. To display computed text, pass it as a string: textView.setText("g");
Edited: Check your XML file, I have test with something very basic and it works
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/txt_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="myTextView"/>
</LinearLayout>
Maybe try to clean your project (Project->Clean in Eclipse), I recently have some trouble with R generation on the last ADT version.
Try this:
textView.setText(Integer.toString(ascii));
Also make sure that your layout xml has TextView txt_1
I've tried :
Integer.toString(char) and String.valueOf(char)
both didnt work.
The only solution is :
txt.setText(""+char);
This is not very efficient from optimization point of view but it does the trick :)

Categories

Resources