4

I want to be able to use the action bar in my android project (2.3), so I am trying to import the actionbarsherlock library. However, I am having problems....

I've imported the library, edited the build path of my android project to include the library, and also deleted the android-support-v4 jar from the ABS /libs directory and copied my project's android-support-v4 into the ABS /libs directory to solve the jar mismatch. However, now that I've done all those things, my project can't resolve myproject.R anywhere. I import myproject.R in almost all of my .java files, and they all have the same error ("The import com.myproject.R cannot be resolved"). In addition, when I clean my project, I get a lot of errors in the console window:

They all go along the lines of:

error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Light'.

To my knowledge, R is where all of the layout information is stored, so I have no idea why this is even a problem...

I've been working on this error for days, and I'm extremely frustrated and finally decided to take it to stack overflow. Thanks guys for the help!

bhsu
  • 392
  • 1
  • 7
  • 17
  • You should not need to add the `import R.` to any of your Java files. Unfortunately you've run into a problem with Eclipse that hits many of us i.e. change something and Eclipse throws a wobbler. Have you tried looking in the window Window->Show Window->Problems? Also you could try cleaning your project. – D-Dᴙum Apr 14 '13 at 19:42
  • Problems importing `R`are usually related with something going wrong in the classpath or classes conflicts; in these cases, the sdk can't create R.java - which is automatically generated each time the project is compiled -. Please, double check **Problems** tab and let us know what you find. – ssantos Apr 14 '13 at 19:43
  • It's wrong to import R .. did you try to clean/rebuild your project before you imported the R file? – Elior Apr 14 '13 at 19:50
  • to clarify, when I say importing R, i mean import "com.portfoliotracker.R," which I'm pretty sure is the R.java that eclipse generates. I'm pretty sure this is right, as I've been doing it thus far in my project. It's just that now that i've included the ABS library as one of my libraries in the project, the line "import com.blah.R" causes an error. – bhsu Apr 14 '13 at 20:00
  • @ssantos In the problems tab, all the problems are related to R (when I try to access something in a layout- R.layout.x, or when I access R.drawable.x) – bhsu Apr 14 '13 at 20:03
  • @moneydev try the below answer. – Raghunandan Apr 14 '13 at 20:30

3 Answers3

4

ActionBarSherlock is a library project.

So import the library project in your eclipse

enter image description here

Its a library project. To check Right Click on your library project. goto Properties.

Choose android. You will see a Is Library checked as shown in the below pic.

enter image description here

Add your library project to your Android project.

Right click on your Android project. Goto Properties . Choose android . Click add button add the library to your project. You can see a green tick in the below pic.

enter image description here

Make sure you have one copy of andorid-suuport-v4.jar in your libs folder.

Do not import R.*;

You will see two R.java one for the library project and one for your android project. Clean and build.

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Hi Raghunandan, thanks for the answer. I did exactly what you said, but in my gen folder, I don't have the com.actionsherlock package, and even in my own project's package, I don't have a R.java (I only have BuildConfig.java). I remember before I had an R.java, but once I add actionsherlock as a library, the R.java just seems to disappear. Any thoughts as to why this is? – bhsu Apr 14 '13 at 20:48
  • @moneydev: "but once I add actionsherlock as a library, the R.java just seems to disappear" -- you have an error in your manifest or a resource -- check the Eclipse Console tab for error messages. – CommonsWare Apr 14 '13 at 20:53
  • @CommonsWare In my Eclipse Console tab, I have A LOT of red error messages all pretty much saying the same thing. They say "...\library\res\values-v14\abs__themes.xml:30: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Dialog'." – bhsu Apr 14 '13 at 21:01
  • 1
    @moneydev: Your Android build target is set too low. Set it to API Level 14 or higher in Project > Properties. – CommonsWare Apr 14 '13 at 21:02
  • OMG THANK YOU SO MUCH. It finally worked!! @CommonsWare I cannot express online how appreciative I am, honestly. Can you add that as an answer so I can upvote you? – bhsu Apr 14 '13 at 21:04
  • sorry to comment so much, but just another question: if I make my project build target 4.0, can I still run it on an emulator/phone running Android 2.3? – bhsu Apr 14 '13 at 21:06
  • @CommonsWare your comment sort of saved my life :) tnx for your contribution and time... – John x Nov 23 '13 at 09:20
0

edited the build path of my android project to include the library

Never manually modify the build path of an Android project.

ActionBarSherlock is an Android library project. You need to add a reference to ActionBarSherlock's library project from your main application project and undo the manual change to the build path.


UPDATE

The second half of your problem was that your build target was set too low. The build target controls what version of the Android classes, resources, etc. is available to your app, and if you use ActionBarSherlock, you need this to be API Level 14 or higher.

if I make my project build target 4.0, can I still run it on an emulator/phone running Android 2.3?

Yes. Set your android:minSdkVersion to be 10 or lower. Eclipse (via Lint) will yell at you if you accidentally try using classes, methods, and such that are higher than your minSdkVersion, even though they are legal due to your build target.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • CommonsWare, thanks for the help! I did add a reference (properties>android>add library). But I did rightclick project> build path> configure>projects>add>select the actionsherlock library, should I not have done that? – bhsu Apr 14 '13 at 20:52
  • @moneydev: "should I not have done that?" -- correct, you should not have done that. – CommonsWare Apr 14 '13 at 20:52
  • Ok, so I went to the build path and took it out. But I still have all of these cannot resolve R errors. FYI, if I remove the ABS library in the project properties, everything is fine (there is an R.java in /gen). But then I add the ABS library in properties, and apply (like the post 2 above said), and then I get all these errors flooding back... – bhsu Apr 14 '13 at 20:59
  • @moneydev: You have an error in your manifest or a resource. Check the Eclipse Console tab for error messages. – CommonsWare Apr 14 '13 at 21:00
  • Thanks for your help so far. I responded to your above comment on the post above. I am having error messages in the Eclipse Console. They all basically say that it can't retrieve parent or something for a bunch of things which look like they are layout/ui related stuff – bhsu Apr 14 '13 at 21:03
0

I found that if minSDKVersion in AndroidManifest.xml of the Project is lower than library, it also make the Project to be error to build R.java

Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71