R cannot be resolved error when using ActionBarShe

2019-02-16 13:40发布

问题:

I am trying to use Jake Wharton's ActionBarSherlock to support actionbars in android projects all the way down to 1.6

I'm getting started using the video provided on the faq's page but after 2:25 in the video, when he imports the project, I get a whole huge list of >200 compilers errors, mostly dealing with some @override stuff. I fix most of those errors by:

Right click on the project>properties>Java Compiler>compiler compliance level: and I set it to 1.6.

This forces a re-build and suddenly I am down to just 54 errors. Most of them are "R cannot be resolved to a variable" but the last 5 are all "the import com.actionbarsherlock.R cannot be resolved". I have searched around and the common ways of solving this worked out as follows:

"Make sure your resources are not in error" -- I checked and there are no problems with anything in the /res folder

"Make sure you aren't importing android.R" -- I am not. I have:

import com.actionbarsherlock.R

It occurred to me that the package name might be incorrect, but it isn't.

"Simply clean the project" -- Have done this many times to no effect

"Make sure eclipse is able to generate R files at all" -- It can in the blank test project I made just now

And some other fixes which are more specific to ActionBarSherlock:

"Make sure your targetSDK is 11 or higher" -- It is set to 13

"Make sure the project is a library project" -- It is

How can I get rid of these errors?

回答1:

Found it!

Instead of:

Project > Clean > Clean Projects Selected Below

you must use

Project > Clean > Clean All Projects

Even if none of the other projects have anything to do with the library project. This fixed the problem



回答2:

My fault was, that the ABS-project and my project had different API levels. ABS was on level 4.0.3 and my project had 3.0. After changing the API level of my project to 4.0.3 all errors were fixed. This post helped me to find the solution.



回答3:

Might also need to make sure your nen/R.java file is created for the right package name:
On AndroidManifest.xml, make sure the manifest tag, package attribute is set to:

"com.actionbarsherlock"



回答4:

Instead of importing the single folder actionbarsherlock, I ended up picking the parent folder and unchecked everything but actionbarsherlock in the Import Projects window.



回答5:

Mine was different:

I have a 64bits OS (linux mint) and I had to install the 32bit libraries:

sudo apt-get install ia32-libs

After this just clean all and build a new project and it worked.



回答6:

  • As usual anytime R file not created you should first try:

Project > Clean > Clean All Projects

  • If that doesn't help, make sure you have set a high enough SDK version, I think it should be at least 14 or higher for ActionBarSherlock:

  • Lastly, go to Android SDK Manager and check if there is a update Android SDK Platform-tools and Android SDK Build-tools to version 17. If yes, update it and RESTART eclipse