DJPlayer
DJPlayer

Reputation: 3294

R cannot be resolved to a variable

it's been a little while since I made an android project..
But recently I upgraded my eclipse version and a number of plugins etc..

Now all my projects won't compile and R cannot be resolved. ugh..

Upvotes: 1

Views: 726

Answers (3)

Abdullah Adeeb
Abdullah Adeeb

Reputation: 4306

I had the same problem, here's the steps to solve: if you have the error in only one activity then jump to step 5. if all the activities have the same error then go step 1.

  1. click on project>>clear.
  2. check the console for errors on the xml or resources files, as if error in the any will prevent the R.java from being generated
  3. for me there was resources that was named with "-" and this caused an error.
  4. After that clear the project again this should fix it.. if not
  5. (this usually cause only on activity of generating this error)make sure your activity does not contain import of R.java
  6. if the problem persists then you have to go for the long dummy way of debugging errors, by copying the project folder somewhere as a backup (This is very important), start deleting layouts one by one...
  7. everytime you delete a layout make sure to delete all the resources from the project that is used by that layout and delete it's id from the manifest and delete the activity associated with it.. (start with the activity that's causing the error).
  8. keep clearing the project after every delete to narrow down the cause of the problem.
  9. grab a cup of coffee; this might take a while, you could also take another approach by creating a new project and import activities, layouts and resources one by one.
  10. good luck

Upvotes: 1

Howard Hodson
Howard Hodson

Reputation: 943

This isn't a good answer. It's just what worked for me.

I just had the same problem a few days ago. Fixing R isn't the answer. I tried for a couple of hours. It won't work. Every id, every attr, every layout, every thing depends on R. Almost every statement in the code is red lined.

To fix it, I had to delete the workspace and start up a new one. Then restore everything from the last working backups. (If you don't already, Export the file structure about every couple of hours. Keep at least the last 3. It will save you someday.)

I think the problem occurred when I was working with the emulator. I was exiting from a hung start. I was going to restart after deleting an emulator that was junked up and recreate it. I exited Eclipse before clicking the 'not responding/cancel' message for the emulator. I didn't see the saving the environment progress bar from Eclipse when I stopped it. I think the workspace metadata or similar was blasted.

Upvotes: 1

nhaarman
nhaarman

Reputation: 100378

Look for problems that are not 'R' - related and fix them (these problems will prevent the R file from being built). Then clean your projects to invoke the generation of the R file.

Upvotes: 0

Related Questions