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.
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.Rto 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.