虽然答案是loeschg https://stackoverflow.com/a/26431677/982852绝对正确,我只是想详细说明一下并给出一个适用于所有 IDE 的解决方案(Eclipse、IntelliJ 和 Android Studio)即使错误略有不同。
先决条件
确保您已下载最新版本extras
以及Android 5.0 SDK
通过 SDK 管理器。
安卓工作室
打开build.gradle
or build.gradle.kts
您的应用程序模块的文件并更改您的compileSdkVersion
到21.基本上不需要改targetSdkVersion
SDK 版本为 21,但建议您这样做,因为您应该始终瞄准最新的 android Build-Version https://stackoverflow.com/a/14939070/982852.
最后你的 gradle 文件将如下所示:
android {
compileSdkVersion(21)
// ...
defaultConfig {
// ...
targetSdkVersion(21)
}
}
请务必随后同步您的项目。
Eclipse
当使用v7-appcompat
在 Eclipse 中,您必须将其用作库项目。仅仅将 *.jar 复制到您的/libs
文件夹。请阅读这个(点击) https://developer.android.com/tools/support-library/setup.html#libs-with-res分步教程开发者.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'.
Solution
您唯一需要做的就是打开project.properties
的文件android-support-v7-appcompat
并将目标从target=android-19
to target=android-21
.
之后只需做一个Project --> Clean...
以使更改生效。
IntelliJ IDEA(不使用 Gradle)
与 Eclipse 类似,仅使用android-support-v7-appcompat.jar
;你必须导入appcompat
作为一个模块。阅读更多相关信息StackO-Post(点击) https://stackoverflow.com/questions/18025942/how-do-i-add-a-library-android-support-v7-appcompat-in-intellij-idea.
(Note: 如果您只使用.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
Solution
右键单击appcompat
模块 --> 打开模块设置 (F4) -->[依赖项选项卡]从下拉列表中选择 Android API 21 平台 --> 应用
然后重建项目(Build --> Rebuild Project)就可以了。