android.support.v7.widget.Toolbar使用支持lib 24+时的VectorDrawableCompat IllegalStateException

时间:2016-07-29 05:20:01

标签: android android-support-library android-toolbar appcompat-v7-r23 appcompat-v7-r24

我已在Android Studio中启动了一个新项目并配置了我的gradle构建设置。当我将android.support.v7.widget.Toolbar添加到布局时,在设计视图中出现以下错误。请注意,此错误仅在使用小于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

这是我的app 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或更高版本的支持库?我不知道有任何其他配置需要更改,因为异常试图指示。

design preview

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

确保使用的gradle版本大于2.0

buildscript {
  ...
  dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0'
  }
}