虽然loeschg的答案是完全正确的,但我想详细阐述一下,并为所有IDE(Eclipse、IntellJ和Android Studio)提供一个解决方案,即使错误略有不同。
先决条件
确保您已通过SDK-Manager下载了最新的extras
和Android 5.0 SDK
。
Android Studio
打开您的应用程序模块的build.gradle
或build.gradle.kts
文件,并将您的compileSdkVersion
更改为21。基本上不需要将targetSdkVersion
SDK版本更改为21,但建议这样做,因为您应始终针对最新的Android Build-Version进行目标设置。
最终 gradle 文件将如下所示:
android {
compileSdkVersion(21)
defaultConfig {
targetSdkVersion(21)
}
}
请务必在此之后同步您的项目。
Eclipse
在Eclipse中使用v7-appcompat
时,您必须将其作为库项目使用。仅将*.jar文件复制到您的/libs
文件夹中是不够的。请阅读developer.android.com上的这个 (点击) 分步教程,以了解如何正确导入项目。
项目导入后,您会发现/res
文件夹中的某些文件夹因以下错误而被红色下划线标记:
error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material'.
error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.*'
error: Error: No resource found that matches the given name: attr 'android:actionModeShareDrawable'.
解决方案
您需要做的唯一一件事就是打开 android-support-v7-appcompat
的 project.properties
文件,将目标从 target=android-19
更改为 target=android-21
。
然后只需执行 Project --> Clean...
即可使更改生效。
IntelliJ IDEA(不使用 Gradle)
与 Eclipse 类似,仅使用 android-support-v7-appcompat.jar
是不够的; 您必须将 appcompat
作为模块导入。 在此StackO-Post (点击)中了解更多信息。
(注意: 如果您仅使用.jar
,则会在运行时遇到NoClassDefFoundErrors
)
当您尝试构建项目时,您将在 res/values-v**
文件夹中遇到问题。您的消息窗口将显示类似以下内容:
Error:android-apt-compiler: [appcompat] resource found that matches the given name: attr 'android:colorPrimary'.
Error:(75, -1) android-apt-compiler: [appcompat] C:\[Your Path]\sdk\extras\android\support\v7\appcompat\res\values-v21\styles_base.xml:75: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ActionButton'.
// and so on
解决方法
右键点击 appcompat
模块 --> 打开模块设置 (F4) --> [依赖项标签] 从下拉菜单中选择 Android API 21 平台 --> 应用
然后重新构建项目 (Build --> Rebuild Project) 即可。