Appcompat Activity 给出版本 24.0.0 的问题

Appcompat Activity Giving issues for version 24.0.0

我想使用 Coordinator Layout,我的 gradle 文件包含以下依赖项,

compile 'com.android.support:appcompat-v7:24.0.1'     
compile 'com.android.support:design:24.0.1'

但这给了我以下错误,

Exception while inflating <vector>
org.xmlpull.v1.XmlPullParserException: Binary XML file line #17<vector> tag requires viewportWidth > 0
        at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:541)
        at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:478)
        at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:441)
        at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:736)
        at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:359)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:189)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:182)
        at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:717)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:187)
        at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
        at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
        at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
        at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
        at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
        at com.android.co_lay_demo.fab_and_snackbar.onCreate(fab_and_snackbar.java:20)
        at android.app.Activity.performCreate(Activity.java:5047)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
        at android.app.ActivityThread.access0(ActivityThread.java:134)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)

如果我将依赖项的版本降级到,

compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'

应用程序运行无误。

我的 Xml 文件看起来像,

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sample.foo.usingcoordinatorlayout.FabAndSnackbarActivity">

<Button
    android:id="@+id/showSnackbarButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="show_snackbar"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:src="@android:drawable/ic_popup_disk_full"/>

根据新版本,我是否遗漏了什么?

替换你gradle版本

classpath 'com.android.tools.build:gradle:2.1.3'

根据Ajinkya posted answer at 您需要执行以下步骤

将此代码添加到您的应用级别 build.gradle 文件

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

对于Gradle 1.5使用

defaultConfig {
        generatedDensities = []
    }

    // This is handled for you by the 2.0+ Gradle Plugin
    aaptOptions {
        additionalParameters "--no-version-vectors"

}

更多详情请访问