Home » Android » eclipse – R cannot be resolved – Android, no import android.R; statement

eclipse – R cannot be resolved – Android, no import android.R; statement

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

I have a few suggestions:

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

  2. Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.

  3. Check for errors in your layout XML files.

Answer:

  1. You can try to “clean” your project.
  2. The default tech solution helps sometimes: restart Eclipse (seriously)
  3. 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.
  4. Remove all “import R” stuff in the import sections. You don’t want that.

Answer:

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.

Answer:

Along with the great suggestions in the previous answers, make sure your Android target is set:

  1. Right-click on your project
  2. Choose Properties
  3. Choose Android in the left menu
  4. Tick a box next to the appropriate Project Build Target.
  5. 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.

Answer:

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

Answer:

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

Answer:

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

Answer:

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 .

Answer:

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.

Answer:

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.