17

I added some UI elements to the main.xml file in the res\layout folder and when I try to access them through the call, R.layout.my_uielement, the UI element that I created is not there. Even when I add a new xml file with UI elements, it still doesn't show up in the R.layout class. I have made checked the ids on them and they have the correct format (I think): android:id="@+id/my_button". What could be the problem? Do I need to compile the code first?
UPDATE:
I have already tried adb kill-server then adb start-server. It doesn't seem to help. I have also tried R.id.my_uielement, it doesn't register either.

4
  • 4
    Have you tried Clean Project in Eclipse? Are the xml files in the right folder?
    – Pentium10
    Mar 21, 2010 at 13:46
  • 1
    yes they are in the res\layout folder. Clean Project doesn't work either. Mar 21, 2010 at 13:55
  • 1
    Upgrade Eclipse ADT plugins. Copy the source into a new folder, and import into a new project. Try recreating the XML file you have. Anyway post the answer if you find out.
    – Pentium10
    Mar 21, 2010 at 14:48
  • 3
    Had the same problem, running "Clean Project" solved it. Jul 23, 2011 at 19:07

8 Answers 8

35

This may not be the case for you, but sometimes when I use something from the android.R package (default android resources) and I use it in my class before making reference to anything from my own resource package, Eclipse adds the import android.R; statement in my imports. This will conflict with the default behavior of implicitly looking in your package's resources.

Check your import statements for import Android.R;. If it's there, remove it. And, if you're using anything from android.R, fully qualify it when you use it in code.

1
  • Brilliant! I wish I can give you 100 up-votes :D I've been looking for an answer for so long. Thanks!
    – marienke
    Feb 27, 2013 at 9:46
9

Clean project works fine. In Eclipse if you just change some xml components, it doesn't always generate a new R file. Cleaning does it.

0
6

Use R.id.your_ui_element, not R.layout.your_element

0
2

I faced this problem too. The R.java was neither generated nor updated.

If R.java is not generated when you create the project, run your 'empty` project. An R.java with references to resources will be automatically generated.

If R.java is not getting updated, Check for updates in Help -> Check for Updates in Eclipse and update your SDK and ADT if required.

2
  • That helped me, in conjunction with cleaning project. Nov 10, 2011 at 18:43
  • @IgorG. I'm glad it helped. I spent 2 days before I came across this question on stackoverflow
    – Pramod
    Nov 11, 2011 at 5:30
2

After 2 days I found a way to fix it...

I had saved half done project in my mail, so when R.java vanished i just replaced it with the old R.java file, this fixed a lot of errors, then made necessary changes and built the project(build it not clean)...I do not know whether clean and build performs the same job or not..but everytime i cleaned it , R.java used to go invisible...

2

Clean is working because it BUILD the project after cleaning. So the problem is ( at least it was in my case, because I switched off the Automatic Build a few days earlier ) that after you add an item or anything in a .xml file, the eclipse doesn't build it and can't refresh the R.java .

1

I've noticed that my R.java file is not always being properly updated when I add items to my strings.xml file or elsewhere. If I use a new value I added like R.string.newString the actual integer value might still be pointing to R.string.oldString or whatever (it seems to usually be the element directly above or below the new element in strings.xml but I haven't nailed down the pattern for sure yet).

You're not supposed to modify the R.java file, but it doesn't say anything about deleting it (not that I've seen, anyway). I've found that deleting R.java usually fixes my resource-related problems because Eclipse immediately regenerates the file with the integer values properly assigned.

Please note, I'm very much a beginner at Eclipse so I may not understand the consequences of deleting R.java but it seems to be working well for me so far. If what I'm doing is very bad, please let me know.

1

None of these worked for me! The only way i can access an @+id/foo is by myPackage.R.id.foo.

Not the answer you're looking for? Browse other questions tagged or ask your own question.