在build.gradle中使用android支持库23.2崩溃app(XmlPullParserException:XmlPullParserException:无效的drawable标签向量)

时间:2016-02-25 07:25:17

标签: android svg custom-controls android-support-library

使用支持库v23.1.1插件并在Android 4.4.4(API 19)下运行时,该应用程序运行正常:

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.1.1'

然而,当我使用newer version (23.2)的android支持库构建它时,它会崩溃:

XmlPullParserException: Binary XML file line #17: invalid drawable tag vector

当应用想要使用简单的自定义视图来充气警报对话框时会发生这种情况。自定义视图使用包含<shape>标记的XML文件作为其drawable之一。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

<solid
    android:color="@android:color/white" >
</solid>

<corners
    android:radius="5dp">
</corners>

<stroke android:width="1dp" android:color="@color/AppColor" />

</shape> 

虽然我无法在崩溃日志中明确指出这个基于SVG的drawable,但我的猜测是因为Google引入了对VectorDrawables的支持,所以它与我的应用程序发生了冲突。

有关如何查明罪魁祸首的任何提示或输入都表示赞赏。

2 个答案:

答案 0 :(得分:13)

我遇到了类似的问题。我得到了同样的错误,因为我在设置ImageView的源代码时仍然使用android:src属性而不是app:srcCompat

在您引导矢量绘图的任何地方更改此属性都可以解决您的问题。

旧:

<ImageView
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:src="@drawable/ic_add" />

新:

<ImageView
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  app:srcCompat="@drawable/ic_add" />

答案 1 :(得分:3)

原来有两个不同的问题:

  • 我在XML文件中使用了普通的RadioButton。将其更改为android.support.v7.widget.AppCompatRadioButton可解决问题。
  • 其次,由于博客文章建议我必须将一些drawable包装成容器 drawables,以便支持库可以在进行基于矢量的绘图时使用它们。例如,在我的一个自定义按钮中,我将基于矢量的背景保存在dialog_bg.xmlbutton_round_bg.xmlbutton_round_bg_2.xml中。

    要解决此问题,我必须将它们放在其中一个容器中(例如State listInsetLevelList)。此包装器已保存到名为vector_wrapper.xlm

  • 的字段中
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/dialog_bg"
        android:maxLevel="1"
        android:minLevel="1" />
    <item
        android:drawable="@drawable/button_round_bg"
        android:maxLevel="2"
        android:minLevel="2" />
    <item
        android:drawable="@drawable/button_round_bg_2"
        android:maxLevel="3"
        android:minLevel="3" />
</level-list>

现在我要将自定义按钮的背景更改为

<customButton
    android:background="@drawable/vector_wrapper"
</customButton>

由于包装器现在有三个drawable,我可以根据我的需要在Java代码中选择它们:

customButton btnSave = (customButton) view.findViewById(R.id.btnSave);
btnSave.getBackground().setLevel(3);

如果你的视图是基于Image的视图{@ 3}} @themichaelscott建议你不需要容器包装器,那么只需将src更改为app:srcCompat="@drawable/ic_add" < / p>