1

I simply tried to import a sample project in the project workspace of Eclipse but without even touching the code, it gives huge number of errors all telling that R.java cannot be converted to a variable. But the file R.java is not present at all in the tree structure and I don't know that where it has gone. Now when I am creating a new Android project, it again gives the same error in that new project as well. I face this problem too often and it is a great hurdle for me..

Can anybody please tell the cause and the solution to the problem? Thanks in advance.

Maria Niaz
  • 107
  • 1
  • 12
  • 1
    Try `Project->Clean->Clean All` – axiom Nov 19 '12 at 21:34
  • Double check your project's project.properties, in some cases target=? is set too low so that Eclipse doesn't complete the compilation hence doesn't generate the R file. This even happened in some Android sample project like APIDemo. – yorkw Nov 20 '12 at 03:34

8 Answers8

3

Some background: R.java is automatically generated from your project's resources. When you add something like a layout or a drawable resource to a project, the Android SDK will compile that for you and attribute a resource ID to it, and the R.java class holds those IDs.

Sometimes when importing a sample project, Eclipse fails (or takes long) to generate the R class based on the resources, and in the meantime you will see that as an error. If you try to fix the error using the auto import fixer (Ctrl+Shift+O), the problem will become worse because Eclipse will add import android.R to your file, which is not correct (remove it if you find this in any of your files).

The solution in this case is usually to clean the project (Project | Clean) and build it again, and it should work just fine.

Notice, however, that if you are seeing several error messages, the true cause of the problem might be hidden -- if there is a syntax error in the XML for one of your resource files, for instance, that might prevent the SDK from generating the resources, which in turn prevents it from generating R.java. So check carefully to see if there are no error message about parse errors on XMLs, drawables, etc.

Hope this helps.

Bruno Oliveira
  • 5,056
  • 18
  • 24
  • I have cleaned the solution and rebuilt it as well. but there is no effect of these things. And also, there is no other error. it's just related to R.java – Maria Niaz Nov 20 '12 at 00:00
  • The names of the images i included in drawable were not according to the naming convention like they contained capital aphabets etc. – Maria Niaz Dec 02 '12 at 18:14
1

maybe you forgot to add related library to your project ?

To go to Screen1 and 2 , right click your project from package explorer then go to properties.

to see Problems Window screen3, Window - > show views -> Problems

Screen 1

Screen 1

Screen 2

Screen 2

Screen 3

Screen

Talha
  • 12,673
  • 5
  • 49
  • 68
1

Change the target=android-X in the project.properties file, replace X with the api level of which the sample project is. For example, if you imported the sample project from sdk-path\samples\android-17 dir, then replace it with target=android-17 and then rebuild the project.

djhs16
  • 471
  • 1
  • 5
  • 12
0

Make sure that your source files don't contain an import like...

import android.R;

Also, check your XML files are correct, as errors in your XML (layouts etc. under res folder) can cause this issue.

Another cause can be when you rename you package but don't update the package name in your AndroidManifest.xml file.

frijj2k
  • 161
  • 1
  • 6
0

Typically a sign of the Android SDK not being installed correctly. Happens to me a lot, I have to repoint to the SDK and include proper libraries.

Mgamerz
  • 2,872
  • 2
  • 27
  • 48
0

Maybe you have an error in resources (res folder). If you have no errors in resources try Project -> Clean.

Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
  • As i told you that I have imported a sample project which were downloaded during installation. Should resource of sample project contain errors? I don't think so... I have tried Cleaning but it makes no difference – Maria Niaz Nov 19 '12 at 23:38
0

I just tested this on my computer and I got it working. Now, this solution does not work for all of the samples. It worked for the project called ApiDemo, but it did not work for MapsDemo. The reason why you are having this problem is because you are not doing the right steps. If you import, as you stated, then you will experience this kind of problem.

Here is the wrong way which would cause this error:

in project explorer - import - existing project into Workspace

if you do it this way, then yes.. your R.java file will NOT be created.

here is the right way of doing it:

click on NEW - project - choose "Android Sample Project" - now you can choose the build target of your choice - select the project you wish and hit finish.

Before you run this project, you must first do this: in Eclipse - go to project - and click on "clean".

Now you will be able to run the project.

Nadir Ahmed
  • 361
  • 2
  • 5
0

Whelp, after researching the hell out of this, nothing seemed to work. Then, in some hole in the wall forum I found a simple yet effective solution. Make sure Eclipse is pointing to the correct SDK directory. I dunno how I did it but I had 2 SDK's installed and the one I was working with looked good but I had eclipse pointing to a random one.

After updating the SDK's path in elcipse, the R.java file began compiling correctly. This is a basic check not suggested by anyone. If you are having this issue, take 15 seconds to double check this. Could save you 3 hours of troubleshooting Eclipse.