问题
I am new to android-programming. I am following this tutorial to add an ActionBar to my Main_Activity using the explanation in the "Support Android 2.1 and Above" section.
I followed this tutorial to add a Library-Project. Then after adding the library, at the point "Add the library to your application project: I felt lost .. basically because I don't have the "add" option by itself in my Eclipse ..
however I clicked on "add library" and moved on ..
now when I change Activity
to ActionBarActivity
:
public class MainActivity extends ActionBarActivity { ... }
in my mainActivity.java , it gives me error (a red line bellow the ActionBarActivity and all methods' names and some functions) .. and again when i change:
<activity android:theme="@style/Theme.AppCompat.Light" ... >
in the Manifest, it also give me an error saying: No resource found that matches the given name ..
I am totally lost .. what did I do wrong ?
回答1:
It does not sound like you imported the library right especially when you say at the point Add the library to your application project: I felt lost .. basically because I don't have the "add" option by itself .. however I clicked on "add library" and moved on ..
in eclipse you need to right click on the project, go to Properties, select Android in the list then Add to add the library
follow this tutorial in the docs
http://developer.android.com/tools/support-library/setup.html
回答2:
Check if you have a android-support-v4.jar
file in YOUR project's lib folder, it should be removed!
In the tutorial, when you have followed the instructions of Adding libraries WITHOUT resources
before doing the coorect Adding libraries WITH resources
you'll get the same error.
(Don't know why someone would do something like that *lookingawayfrommyself* ^^)
So what did fix it in my case, was removing the android-support-v4.jar
from YOUR PROJECT (not the android-support-v7-appcompat project), since this caused some kind of library collision (maybe because in the meantime there was a new version of the suport library).
Just another case, when this error might shows up.
回答3:
This way work for me with Eclipse in Android developer tool from Google -righ click - property - java build path - add external JAR
point to: android-support-v7-appcompat.jar in /sdk/extras/android/support/v7/appcompat/libs
Then
import android.support.v7.app.ActionBarActivity;
回答4:
I was also following the instructions on http://developer.android.com/training/basics/actionbar/setting-up.html
and even though I did everything in the tutorial, as soon as "extends Action" is changed to "extends ActionBarActivity" all sorts of errors appear in Eclipse, including the "ActionBarActivitycannot be resolved to a type"
None of the above solutions worked for me, but what did work is adding this line to the top:
import android.support.v7.app.ActionBarActivity;
回答5:
Instead of copy/pasting the code from the tutorial, use the code suggestion in the IDE. Start typing "extends ActionBar..." it will propose "ActionBarActivity" click enter. It worked for me!
回答6:
UPDATE:
Since the version 22.1.0, the class ActionBarActivity is deprecated, so instead use AppCompatActivity. For more details see here
Using ActionBarActivity:
In Eclipse:
1 - Make sure library project(appcompat_v7) is open & is proper referenced (added as library) in your application project.
2 - Delete android-support-v4.jar from your project's libs folder(if jar is present).
3 - Appcompat_v7 must have android-support-v4.jar & android-support-v7-appcompat.jar inside it's libs folder. (If jars are not present copy them from /sdk/extras/android/support/v7/appcompat/libs folder of your installed android sdk location)
4- Check whether ActionBarActivity is properly imported.
import android.support.v7.app.ActionBarActivity;
In Android Studio
Just add compile dependencies to app's build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
回答7:
Add this line to dependencies in build.gradle
:
dependencies {
compile 'com.android.support:appcompat-v7:18.0.+'
}
Reference
回答8:
I got the same problem, but things got complicated when I added few other libraries like appcompat.v7, recyclerView, CardView.
Removing appcompat.v4 from lib did not work for me.
I had to create project from start and first step I did is to remove appcompat.v4 from libs folder, and this worked.
I had just started the project so creating a new project wasn't a big issue for me!!!
回答9:
There is a mistake in your 'andrroid-sdk' folder.
You selected some features while creating new project which need some components to import.
It is needed to download a special android library and place it in android-sdk
folder.
For me it works fine:
1-Create a folder with name extras
in your android-sdk
folder
2-Create a folder with name android
in extras
3-Download this file.(In my case I need this library)
4-Unzip it and copy the content (support folder) in the current android
folder
5-close Eclipse
and start it again
6-create your project again
I hope it to work for you.
回答10:
For Eclipse, modify project.properties
like this: (your path please)
android.library.reference.1=../../../../workspace/appcompat_v7_22
And remove android-support-v4.jar
file in your project's libs
folder.
来源:https://stackoverflow.com/questions/18830736/actionbaractivity-cannot-be-resolved-to-a-type