矢量drawable InflateException

时间:2017-11-09 08:27:40

标签: android android-imageview android-vectordrawable

我有矢量drawable文件(在模拟器API版本25上启动):

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="500dp"
    android:height="500dp"
    android:viewportWidth="500"
    android:viewportHeight="500">

    <path
        android:fillColor="#FFFFFF"
        android:strokeColor="#000000"
        android:strokeWidth="6"
        android:strokeMiterLimit="10"
        android:pathData="M 254.9 162.9 C 293.836074863 162.9 325.4 194.463925137 325.4 233.4 C 325.4 272.336074863 293.836074863 303.9 254.9 303.9 C 215.963925137 303.9 184.4 272.336074863 184.4 233.4 C 184.4 194.463925137 215.963925137 162.9 254.9 162.9 Z" />
    <path
        android:fillColor="#000000"
        android:pathData="M 231 204.3 C 240.223155322 204.3 247.7 211.776844678 247.7 221 C 247.7 230.223155322 240.223155322 237.7 231 237.7 C 221.776844678 237.7 214.3 230.223155322 214.3 221 C 214.3 211.776844678 221.776844678 204.3 231 204.3 Z" />
    <path
        android:fillColor="#000000"
        android:pathData="M 279 204.3 C 288.223155322 204.3 295.7 211.776844678 295.7 221 C 295.7 230.223155322 288.223155322 237.7 279 237.7 C 269.776844678 237.7 262.3 230.223155322 262.3 221 C 262.3 211.776844678 269.776844678 204.3 279 204.3 Z" />
    <path
        android:fillColor="#FFFFFF"
        android:pathData="M 279 207.2 C 283.694420374 207.2 287.5 213.691871127 287.5 221.7 C 287.5 229.708128873 283.694420374 236.2 279 236.2 C 274.305579626 236.2 270.5 229.708128873 270.5 221.7 C 270.5 213.691871127 274.305579626 207.2 279 207.2 Z" />
    <path
        android:fillColor="#FFFFFF"
        android:pathData="M 231 213.2 C 239.008128873 213.2 245.5 217.005579626 245.5 221.7 C 245.5 226.394420374 239.008128873 230.2 231 230.2 C 222.991871127 230.2 216.5 226.394420374 216.5 221.7 C 216.5 217.005579626 222.991871127 213.2 231 213.2 Z" />
    <path
        android:fillColor="#000000"
        android:pathData="M 231.3 221.1 C 235.552592574 221.1 239 224.547407426 239 228.8 C 239 233.052592574 235.552592574 236.5 231.3 236.5 C 227.047407426 236.5 223.6 233.052592574 223.6 228.8 C 223.6 224.547407426 227.047407426 221.1 231.3 221.1 Z" />
    <path
        android:fillColor="#000000"
        android:pathData="M 277.2 221.7 C 281.452592574 221.7 284.9 225.147407426 284.9 229.4 C 284.9 233.652592574 281.452592574 237.1 277.2 237.1 C 272.947407426 237.1 269.5 233.652592574 269.5 229.4 C 269.5 225.147407426 272.947407426 221.7 277.2 221.7 Z" />
    <path
        android:fillColor="#000000"
        android:pathData="M 254.9 237.7 C 263.073814297 237.7 269.7 249.564454129 269.7 264.2 C 269.7 278.835545871 263.073814297 290.7 254.9 290.7 C 246.726185703 290.7 240.1 278.835545871 240.1 264.2 C 240.1 249.564454129 246.726185703 237.7 254.9 237.7 Z" />
</vector>

我将文件添加到ImageView

<ImageView
    android:id="@+id/authorization_icon"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="@dimen/authorization_icon_margin"
    android:layout_marginBottom="@dimen/authorization_icon_margin"
    app:srcCompat="@drawable/rocketv"/>

当我试图给布局充气时

setContentView(R.layout.activity_auth);

我发现了错误:

android.view.InflateException: Binary XML file line #0: Error inflating class ImageView

但Android工作室定义我的图片没有任何问题。我可以在activity_auth布局中看到它。

2 个答案:

答案 0 :(得分:1)

解决方案1:

要从java中使用VectorDrawable或将其用作背景(也在xml中),您需要intimate AppCompatDelegateenable compat vector from resource。以下是该代码。

static
 {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
 }

所以这样定义

public class MainActivity extends AppCompatActivity {

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

OnCreate方法的上方。

解决方案2:

每当你要从java使用VectorDrawable或将其用作背景时(也在xml中),记得使用AppCompatView而不是普通视图,这里我使用了AppCompatImageView

答案 1 :(得分:0)

尝试如下。 用{替换app:srcCompat="@drawable/rocketv" android:src="@drawable/rocketv"

<ImageView
    android:id="@+id/authorization_icon"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="@dimen/authorization_icon_margin"
    android:layout_marginBottom="@dimen/authorization_icon_margin"
    android:src="@drawable/rocketv"/>