17

Every time in code that I have an R, I get an error R cannot be resolved to a variable.

The R.java file appears to be missing.

I have tried cleaning the project.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
James
  • 251
  • 1
  • 3
  • 5

12 Answers12

25

This can happen when you have an error in one of your xml files (for example, a resource cannot be found). This can also happen because of using menus (I have had this problem because of menus, and I had to remove them, clean and add them again). And another possible reason is that you initially wanted to have your R class not in the default place where it is created but in your own package. So it is possible that R is created in its default location and your program is looking for it somewhere else.

superM
  • 8,605
  • 8
  • 42
  • 51
  • 1
    XML doesn't have any error. No YELLOW or RED warning and I still manually checked those lines and found no error. Actually the code was working just fine and stopped working after I cleaned it, because R.java was gone. – amar May 23 '13 at 08:40
  • 1
    Thank you! I did have an error in my XML files that I didnt spot. Fixed the xml error and now I see R.ID back again :) – user952342 Jun 30 '13 at 13:04
  • +1 for the menu thing... Deleting my menu and readding it worked. – teynon Jun 20 '14 at 13:24
11

Check your XML resources (especially these recently edited) for mistakes and sytax errors. When you have an error inside one of these files, the R class autogeneration will abort.

7

Ah, the "R cannot be resolved" error. I think every Android programmer has run across this sometime or the other. I wish the tools were more robust about generation of R.java & class.

Try this, it has worked for me in past:

Right-click on the Project root --> Android Tools --> Fix Project Properties

OceanBlue
  • 9,142
  • 21
  • 62
  • 84
3

This happened to me because of a bad resource name (some audio file had " - " in a name), I fixed it by renaming the file.

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
DziDza
  • 31
  • 1
2

That happens when the project cannot be build, because the project name may not be sufficient for building.

You need to create another project without any unacceptable symbols and copy all code files (.java, xml files, etc) and paste it to newly created project. Then clear your project (project -> clean) and select the build automatically option.

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
2

Check if you accidentally imported android.R, this gives an error with resources as well.

Lars
  • 4,082
  • 2
  • 20
  • 20
0

This happened in my case as a image that was present in res/drawable-hdpi had a capital character, I made it small then it worked.

Anubha
  • 1,345
  • 6
  • 23
  • 35
0

You should definitely check your XML files as most people here post. This is because this problem is most likely because of something in your resources not being right, possibly due to debugging. HOWEVER, if this is not the case you may want to check for folders with any resources that have any CAPITAL LETTERS. also you want to make sure you are using the roman alphabet to name resources properly. This made me scratch my head a little bit because a lot of my folders and settings are in Chinese. I saved some projects to a workspace in a folder in Dropbox named 麥思樂的安卓計劃. I changed the name of the folder and problem solved.

GuGaoPlex
  • 1
  • 2
0

Had the same problem but cleaning the project didn't solve anything even with correct xml. This is what I did

a) Make sure the java builders are checked as in the pic below. If they are , uncheck and check again

b) Insert a dummy value eg in layout add a button

c) Clean project

Java builder configuration

back2back
  • 113
  • 11
0

I found solution:

Check is there any <string> </string> is that you forgot to define in string.xml while copying file from outside.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

This is a very common issue, this is encountered when we edit any of the xml file in our project like drawable, layout, strings etc.

We usually clean project (or) restart our IDE (or) import R.java when we get this problem. Never do import.

First try to fix the errors in all xml files and then remove the R.java import from java files. And then clean the project.

So simple.

Sravani
  • 528
  • 1
  • 8
  • 13
0

If you do not see R.java file generation upon creating an Android project, then you probably have missed downloading Android SDK Tools in the Android SDK Manager. In eclipse, go to Window-->select Android SDK Manager Once the Android SDKManager window opens, check the Tools folder and install. Once the installation is completed, restart the eclipse IDE. Build your project and you can see the R.java file generated under the gen folder of your Android project.

Nadeem
  • 121
  • 1
  • 2