Java ActionBarActivity:无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18830736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionBarActivity: cannot be resolved to a type
提问by hashDefine
I am new to android-programming. I am following thistutorial to add an ActionBarto my Main_Activity using the explanation in the "Support Android 2.1 and Above" section.
我是 android 编程的新手。我正在按照本教程使用“支持 Android 2.1 及更高版本”部分中的说明将ActionBar添加到我的 Main_Activity 。
I followed thistutorial 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 ..
我按照本教程添加了一个库项目。然后在添加库后,在这一点上“将库添加到您的应用程序项目:我感到迷茫..基本上是因为我的 Eclipse 中没有“添加”选项本身..
however I clicked on "add library" and moved on ..
但是我点击了“添加库”并继续..
now when I change Activity
to ActionBarActivity
:
现在当我Activity
改为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:
在我的 mainActivity.java 中,它给了我错误(在 ActionBarActivity 和所有方法的名称和一些函数下方有一条红线).. 当我更改时再次出现:
<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 ?
我完全迷路了..我做错了什么?
采纳答案by tyczj
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 ..
这听起来不像你导入的图书馆是正确的,尤其是当你说 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 Androidin the list then Addto add the library
在 Eclipse 中,您需要右键单击项目,转到属性,在列表中选择Android,然后添加以添加库
follow this tutorial in the docs
按照文档中的本教程进行操作
http://developer.android.com/tools/support-library/setup.html
http://developer.android.com/tools/support-library/setup.html
回答by ahetman
I was also following the instructions on http://developer.android.com/training/basics/actionbar/setting-up.html
我还按照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"
即使我在教程中做了所有事情,一旦“扩展操作”更改为“扩展 ActionBarActivity”,Eclipse 中就会出现各种错误,包括“ActionBarActivity 无法解析为类型”
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;
导入 android.support.v7.app.ActionBarActivity;
回答by Levite
Check if you have a android-support-v4.jar
file in YOUR project's lib folder, it should be removed!
检查android-support-v4.jar
项目的 lib 文件夹中是否有文件,应该将其删除!
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.
在本教程中,当您按照Adding libraries WITHOUT resources
执行 coorect 之前的说明进行操作时,Adding libraries WITH resources
您将遇到相同的错误。
(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).
那么在我的情况下修复它的方法是android-support-v4.jar
从您的项目(不是 android-support-v7-appcompat 项目)中删除它,因为这导致了某种库冲突(可能是因为在此期间有一个新版本的支持图书馆)。
Just another case, when this error might shows up.
只是另一种情况,当此错误可能出现时。
回答by LE SANG
This way work for me with Eclipse in Android developer tool from Google -righ click - property - java build path - add external JAR
这种方式适用于我在 Android 开发工具中使用 Eclipse 来自 Google -右键单击 - 属性 - java 构建路径 - 添加外部 JAR
point to: android-support-v7-appcompat.jar in /sdk/extras/android/support/v7/appcompat/libs
指向:/sdk/extras/android/support/v7/appcompat/libs中的android-support-v7-appcompat.jar
Then
然后
import android.support.v7.app.ActionBarActivity;
回答by ufukomer
回答by Rom Rom
Instead of copy/pastingthe 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!
不要复制/粘贴教程中的代码,而是使用 IDE 中的代码建议。开始输入“extends ActionBar...”,它会建议“ActionBarActivity”点击回车。它对我有用!
回答by Harshil
I got the same problem, but things got complicated when I added few other libraries like appcompat.v7, recyclerView, CardView.
我遇到了同样的问题,但是当我添加了一些其他库(如 appcompat.v7、recyclerView、CardView)时,事情变得复杂了。
Removing appcompat.v4 from lib did not work for me.
从 lib 中删除 appcompat.v4 对我不起作用。
I had to create project from start and first step I did is to remove appcompat.v4 from libs folder, and this worked.
我必须从头开始创建项目,我做的第一步是从 libs 文件夹中删除 appcompat.v4,这奏效了。
I had just started the project so creating a new project wasn't a big issue for me!!!
我刚刚开始这个项目,所以创建一个新项目对我来说不是什么大问题!!!
回答by Amrit Pal Singh
UPDATE:
更新:
Since the version 22.1.0, the class ActionBarActivityis deprecated, so instead use AppCompatActivity. For more details see here
自 22.1.0 版本起,类ActionBarActivity已弃用,因此改用AppCompatActivity。有关更多详细信息,请参阅此处
Using ActionBarActivity:
使用 ActionBarActivity:
In Eclipse:
在 Eclipse 中:
1 - Make sure library project(appcompat_v7) is open & is proper referenced (added as library) in your application project.
1 - 确保库项目 (appcompat_v7) 已打开并且在您的应用程序项目中被正确引用(添加为库)。
2 - Delete android-support-v4.jar from your project's libs folder(if jar is present).
2 - 从项目的 libs 文件夹中删除 android-support-v4.jar(如果存在 jar)。
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)
3 - Appcompat_v7 的 libs 文件夹中必须有 android-support-v4.jar 和 android-support-v7-appcompat.jar。(如果 jars 不存在,请从您安装的 android sdk 位置的 /sdk/extras/android/support/v7/appcompat/libs 文件夹中复制它们)
4- Check whether ActionBarActivity is properly imported.
4- 检查 ActionBarActivity 是否正确导入。
import android.support.v7.app.ActionBarActivity;
In Android Studio
在 Android Studio 中
Just add compile dependencies to app's build.gradle
只需将编译依赖项添加到应用程序的 build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
回答by ganji
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-Downloadthis 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
您的“android-sdk”文件夹中存在错误。您在创建需要导入一些组件的新项目时选择了一些功能。需要下载一个特殊的android库并将其放在android-sdk
文件夹中。对我来说它工作正常:1-extras
在您的android-sdk
文件夹中创建一个名称为文件夹 2-创建一个名称android
为extras
3-下载此文件的文件夹。(在我的情况下,我需要这个库) 4-解压缩它并复制内容(支持文件夹) 在当前android
文件夹中 5-关闭Eclipse
并再次启动它 6-再次创建您的项目
I hope it to work for you.
我希望它对你有用。
回答by HackNone
For Eclipse, modify project.properties
like this: (your path please)
对于 Eclipse,修改project.properties
如下:(请提供您的路径)
android.library.reference.1=../../../../workspace/appcompat_v7_22
And remove android-support-v4.jar
file in your project's libs
folder.
并删除android-support-v4.jar
项目libs
文件夹中的文件。