Ok, I am working on a new app and everything worked fine as long as I used a Relative View. However, I want a tabbed layout so I switched what I had (not much so far since I just got it reading from a DB and settings working) over to a tabbed view. Since that time, any class that has any R. statement in it has an "R cannot be resolved" error. I am following the tutorial from the Android "Hello Views" tutorial so I am assuming that isn't the issue (but it could still be).
10 Answers
I have a few suggestions:
Make sure you don't have any other errors other than the R-related errors. Right-click your project folder in Eclipse, Android Tools -> Fix Project Properties.
Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.
Check for errors in your layout XML files.

- 31,712
- 14
- 72
- 77

- 10,900
- 6
- 43
- 55
-
2Found out that I had one missing character in my res/layout/main.xml file. – smccloud Apr 27 '11 at 15:49
-
+1 to eliminating non-R-related errors first. I still haven't quite figured it out, but I had non-R-related errors in a different project and I can't understand how they interacted -- but clearing up those errors first made the R errors go away. Go figure. – user435779 Dec 30 '13 at 15:42
- You can try to "clean" your project.
- The default tech solution helps sometimes: restart Eclipse (seriously)
- If you have an error (something in your XML maybe, or something else), R cannot be compiled. If this is the case, try to find the first error noted. If you fix this, R will be compiled and found. If necesairy, use point 1. and 2. after fixing stuff.
- Remove all "import R" stuff in the import sections. You don't want that.

- 64,065
- 16
- 119
- 163
-
You don't want to import android.r but you do want to import the R that is part of your project. – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Apr 25 '11 at 17:29
-
No you do NOT want to import Any R as has been stated further down, sometimes Eclipse comes up with the wrong classpath, and it will cause R to derive from android.R instead of yourpackage.R -- cleaning and restarting (and occasionally using autocomplete to make certain it is using yourpackage.R instead of android.R) are the solutions. Again, R is (should be) built as part of your project and should not be imported. – Dan Apr 25 '11 at 17:38
-
When I don't import R from com.mypackage.r I get an error. Is this because my launch activity is in a separate package? – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Apr 25 '11 at 23:31
-
I don't know. If restarting and cleaning and fix project etc do not work there probably is some error. It has nothing to do with R an sich: there is an error in your code that is used to generate R, and so it is not generated. And if it is not generated, it kan not be found. Look for NON-"R" problems. – Nanne Apr 26 '11 at 05:31
-
My project doesn't work if I don't import r for anything outside of the package with my launch activity. – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz May 01 '11 at 05:54
-
@zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz If you've split your app into packages so that the activity/whatever is in a different package to your "application package", you WILL need to import your project's R. If it's in the same package, you shouldn't import it. – just me Dec 06 '13 at 09:28
-
@Dan meant to tag you in prev comment but SO only allows one at a time – just me Dec 06 '13 at 09:31
If you're using Eclipse, trying giving it a kick by doing a Project/Clean and re-building your project. It sometimes has random trouble with the classpath on Android projects.

- 624
- 4
- 10
Along with the great suggestions in the previous answers, make sure your Android target is set:
- Right-click on your project
- Choose Properties
- Choose Android in the left menu
- Tick a box next to the appropriate Project Build Target.
- Click Apply and OK
Edit: A year later I found another cause. I had a .jpg image in my drawable folder with the same name as a .png image. Referencing this image in my code must have confused the program and it gave the "R cannot be resolved" error.

- 393
- 8
- 17
I had the sample problem too and this worked for me.
1.) Check for any errors in your Layout XML. Especially when it comes to text and titles
You should use
android:text="@string/hello"
instead of
android:text="hello"
.2) Clean your project
.3) Restart Eclipse

- 348
- 2
- 9
Check that all of the strings you think you're using (e.g. "@string/Hello_world") actually exist in your strings.xml file. I got the OP's error after declaring a menu item using android:title="@string/Navigation"
, but I hadn't added <string name="Navigation">Navigation</string>
to strings.xml

- 492
- 1
- 7
- 15
You should import your own projects R class rather than androids default R class ie.
your.app.package.R
This will make the values defined under the res folder in your class . After that refresh and then clean your project .

- 11
- 6
In my case it was caused since I refactored my project and moved some classes inside a folder structure. Those classes couldnt find the R as usual, since its based on the root source folder.

- 142
- 5
If anything in your 'res' directory is named using illegal characters, this can happen.
Note that android only allows lower case letters (no capitals!!!), numerals, and the underscore. THAT'S ALL!
Almost every operating system allows other characters, so moving a file into your 'res' directory can easily create this problem.
Fix? Rename the file. This is done by right-clicking the file in your package or project explorer and selecting Refactor->Rename. You may need to clean your project after, but beware of the dreaded "import android.R" that may creep in.
Good luck! -scott

- 11,034
- 6
- 68
- 83
You need to make sure you import R. If your main package is com.example try importing "com.example.R" at the top of your files. For some reason eclipse does not do this for you.

- 5,711
- 8
- 54
- 70
-
You do NOT need "import R", and you should not do this. THe problem is that R is not compiled because of some error (or just randomish stuff that could be fixed by cleaning). – Nanne Apr 25 '11 at 17:17
-
When I have an activity in a separate package than my launch activity I need to "import R", otherwise my project gives me an error. I have tried cleaning and rebuilding. – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Apr 25 '11 at 23:30