tags:

views:

47

answers:

3

In some code we're trying out, coming from various tutorials, we end up missing R.id, which should be generated in R.java, obviously. We can add it in (by analogy with other "first" Android projects we've done), but as this file is auto-generated, anything we do like that just gets overwritten ultimately.

public static final class id
{
   public static final int Button01=0x7f060004;
   .
   .
   .
}

Was there a construct to put into strings.xml, main.xml, etc. that causes this to be generated?

(Yeah, we're total noobs. Sorry.)

Thanks for any help you can give,

Russ Bateman

+1  A: 

Say I have an XML file with the following content:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  >
 <ListView android:id="@+id/ListView01" 
  />
</RelativeLayout>

I save it in res/layout/ . Then R.id.ListView01 gets automatically created.

You might want to look at the Notebook Sample application and how it's organised. At least that's how I got familiar with androids organisation.

Christian
Once I had "@+id..." constructs in my main.xml, cleaning (and rebuilding) saw the end of these missing R.id errors. However, my colleague was not so lucky. We'll press on. Thanks.
Russ Bateman
A: 

R.java is auto generated by Eclipse. If it is not built, this means you have probably an error somewhere in your xml files, or a resource with a name that is not allowed.
Sometimes it is just Eclipse doing strange things, in this case, you can try :
Project > Clean > all project Then let Eclipse work.
Sometimes it solves the issue. If not, it's highly probable that you have an error somewhere in your resources.
To create this file, Eclipse gathers the ids you declared in your xml files with @+id,but also the layout names, images names, string names, ...

Sephy
Or, if you're not using Eclipse, it's generated by the aapt tool, usually via Ant.
Dan Dyer
This is very helpful, thank you, I think we see whence the R.id.whatever is supposed to come now.
Russ Bateman
A: 

It appears that project/res/layout/main.xml contains the constructs that lead to the generation of id in R.java. (I'm not limiting the source for these only to that XML file.)

Look specifically for android:id in various widgets (I think they're called) such as TableLayout, TextView, EditText, etc. There will be corresponding

public static final class id
{
   public static final int x=0x7f05006;
   public static final int y=0x7f05000;
   public static final int z=0x7f0500c;
   .
   .
   .
}

where x, y, z correspond to the (TableLayout, TextView, EditText, etc.) identifier coded immediately after the "@+id/" construct in the XML file (and the initialized value in R.java is magically generated--you don't have to worry about that).

Thanks to all who contributed.

Russ Bateman