我在 Android Studio 中启动了一个新项目并配置了我的 gradle build设置。当我将 android.support.v7.widget.Toolbar 添加到布局时,在设计 View 中出现以下错误。请注意,此错误仅在使用小于 21 的 API(特别是 API 16 - 19)进行预览时出现。当我使用 API 21 - 24 预览时一切正常。
我看过很多解决方案并尝试过,但没有一个能解决问题。我尝试做的一件事是将其添加到我的 gradle 中:
vectorDrawables.useSupportLibrary = true
但这并没有解决问题。我尝试将 compiledSdkVersion 配置为 23,将 buildToolsVersion 配置为 23.0.3。这并没有改变任何东西。如果我将支持库版本更改为 23.4.0 或更低版本,那么问题就解决了。一旦我将它改回 24.0.0 或更高版本,它就会返回。
清除缓存、清理项目并重建、使缓存失效并重新启动都没有解决问题。
The following classes could not be instantiated:
- android.support.v7.widget.Toolbar (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when in the IDE
Exception Details
java.lang.IllegalStateException: This app has been built with an incorrect configuration.
Please configure your build for VectorDrawableCompat.
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:692)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:67)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:298)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:229)
...
invalid drawable tag vector
这是我的应用程序 build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1" // https://developer.android.com/studio/releases/build-tools.html
defaultConfig {
applicationId "org.path.path"
minSdkVersion 16
targetSdkVersion 23
versionCode 0
versionName "0.0.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
ext {
supportLibVersion = '24.0.0' // https://developer.android.com/topic/libraries/support-library/revisions.html
}
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile "com.android.support:design:${supportLibVersion}"
compile "com.android.support:cardview-v7:${supportLibVersion}"
compile "com.android.support:recyclerview-v7:${supportLibVersion}"
}
我的项目 build.gradle 正在使用 classpath 'com.android.tools.build:gradle:2.1.2'
这是工具栏 xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?attr/actionBarSize"
android:elevation="4dp"
android:background="@color/colorPrimary"
app:theme="@style/ActionBarThemeOverlay"
app:popupTheme="@style/ActionBarPopupThemeOverlay"/>
最后,我的 styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- ActionBar -->
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Dark" />
<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColorPrimary">@color/text_on_primary</item>
<item name="android:textColorSecondary">@color/subtitle_on_primary</item>
</style>
这是一个错误,还是我无法使用 24 或更高版本的支持库?我不知道有任何其他配置要更改,因为异常试图指示。
最佳答案
关于android.support.v7.widget.Toolbar VectorDrawableCompat 使用支持库时出现 IllegalStateException 24+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38650995/