资源$ NotFoundException在API22上使用矢量图像但在API24上不使用

时间:2017-12-29 20:09:20

标签: java android svg vector

我在Android中使用vector时遇到了问题。

我正在使用矢量(来自android studio中导入的SVG)使用Textview

将图像显示为android:background="@drawable/ic_blue2"的背景

虽然在使用API​​24的手机上进行测试时一切运行良好,但应用程序在使用带有InflateException和Resources $ NotFoundException的API22的手机上启动时崩溃。

我尝试了以前帖子的答案,例如添加到Gradle vectorDrawables.useSupportLibrary = true,但它没有用。

我正在使用Android Studio 3.0.1和Gradle 3.0.1。请注意,我的活动扩展了“活动”,而不是AppCompatActivity(不知道这里是否重要)。

以下是布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackgroundWhite"
    tools:context="jdev.com.handtwist.MainMenu">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_gray7"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textViewAppname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_blue7"
                android:padding="10dp"
                android:text="test"
                android:textAlignment="center"
                android:textColor="@color/colorBackgroundWhite"
                android:textSize="36sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingBottom="30dp"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:paddingTop="20dp"
                tools:layout_editor_absoluteY="100dp">

                <Button
                    android:id="@+id/buttonSolo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_blue2"
                    android:padding="10dp"
                    android:text="@string/solo"
                    android:textAlignment="center"
                    android:textColor="@color/colorBackgroundWhite"
                    android:textSize="24sp" />

                <Button
                    android:id="@+id/buttonMulti"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/ic_blue2"
                    android:padding="10dp"
                    android:text="@string/multi"
                    android:textAlignment="center"
                    android:textColor="@color/colorBackgroundWhite"
                    android:textSize="24sp" />

            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewVersion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Version"
            android:textAlignment="center"
            android:textColor="@color/colorBackgroundWhite" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

我尝试用作背景的xml示例(文件太大)https://pastebin.com/DRR5eRm0

API22上的错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: jdev.com.handtwist, PID: 4666
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{jdev.com.handtwist/jdev.com.handtwist.MainMenu}: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3160)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275)
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:145)
                      at android.app.ActivityThread.main(ActivityThread.java:7007)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                   Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                      at android.view.LayoutInflater.createView(LayoutInflater.java:640)
                      at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
                      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:450)
                      at android.app.Activity.setContentView(Activity.java:2400)
                      at jdev.com.handtwist.MainMenu.onCreate(MainMenu.java:40)
                      at android.app.Activity.performCreate(Activity.java:6609)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:145) 
                      at android.app.ActivityThread.main(ActivityThread.java:7007) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Constructor.newInstance(Native Method)
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                      at android.view.LayoutInflater.createView(LayoutInflater.java:614)
                      at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:450) 
                      at android.app.Activity.setContentView(Activity.java:2400) 
                      at jdev.com.handtwist.MainMenu.onCreate(MainMenu.java:40) 
                      at android.app.Activity.performCreate(Activity.java:6609) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:145) 
                      at android.app.ActivityThread.main(ActivityThread.java:7007) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                   Caused by: android.content.res.Resources$NotFoundException: File res/drawable-anydpi-v21/ic_gray7.xml from drawable resource ID #0x7f0603a5
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:4061)
                      at android.content.res.Resources.loadDrawable(Resources.java:3929)
                      at android.content.res.Resources.loadDrawable(Resources.java:3779)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:776)
                      at android.view.View.<init>(View.java:4001)
                      at android.view.ViewGroup.<init>(ViewGroup.java:513)
                      at android.widget.LinearLayout.<init>(LinearLayout.java:199)
                      at android.widget.LinearLayout.<init>(LinearLayout.java:195)
                      at android.widget.LinearLayout.<init>(LinearLayout.java:191)
                      at java.lang.reflect.Constructor.newInstance(Native Method) 
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                      at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
                      at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:450) 
                      at android.app.Activity.setContentView(Activity.java:2400) 
                      at jdev.com.handtwist.MainMenu.onCreate(MainMenu.java:40) 
                      at android.app.Activity.performCreate(Activity.java:6609) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:145) 
                      at android.app.ActivityThread.main(ActivityThread.java:7007) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                   Caused by: android.content.res.Resources$NotFoundException: File res/drawable-anydpi-v22/$ic_gray7__0.xml from color state list resource ID #0x7f060118
                      at android.content.res.Resources.twLoadColorStateList(Resources.java:4179)
                      at android.content.res.Resources.loadColorStateList(Resources.java:4117)
                      at android.content.res.TypedArray.getColor(TypedArray.java:397)
                      at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1439)
                      at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1418)
                      at android.graphics.drawable.VectorDrawable.inflateInternal(VectorDrawable.java:544)
                      at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:460)
                      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1192)
                      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1086)
                    at android.content.res.Resources.loadDraw

感谢任何可以帮助我的人。

编辑:尝试使用Ben的方法错误时:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: jdev.com.handtwist, PID: 16157
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{jdev.com.handtwist/jdev.com.handtwist.MainMenu}: android.content.res.Resources$NotFoundException: File res/drawable-anydpi-v21/ic_testb.xml from drawable resource ID #0x7f0603ce
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3160)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275)
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:145)
                      at android.app.ActivityThread.main(ActivityThread.java:7007)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                   Caused by: android.content.res.Resources$NotFoundException: File res/drawable-anydpi-v21/ic_testb.xml from drawable resource ID #0x7f0603ce
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:4061)
                      at android.content.res.Resources.loadDrawable(Resources.java:3929)
                      at android.content.res.Resources.getDrawable(Resources.java:1991)
                      at android.content.res.Resources.getDrawable(Resources.java:1973)
                      at android.content.Context.getDrawable(Context.java:409)
                      at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:351)
                      at jdev.com.handtwist.MainMenu.onCreate(MainMenu.java:63)
                      at android.app.Activity.performCreate(Activity.java:6609)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:145) 
                      at android.app.ActivityThread.main(ActivityThread.java:7007) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                   Caused by: android.content.res.Resources$NotFoundException: File res/drawable-anydpi-v22/$ic_testb__0.xml from color state list resource ID #0x7f060295
                      at android.content.res.Resources.twLoadColorStateList(Resources.java:4179)
                      at android.content.res.Resources.loadColorStateList(Resources.java:4117)
                      at android.content.res.TypedArray.getColor(TypedArray.java:397)
                      at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1439)
                      at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1418)
                      at android.graphics.drawable.VectorDrawable.inflateInternal(VectorDrawable.java:544)
                      at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:460)
                      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1192)
                      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1086)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:4045)
                      at android.content.res.Resources.loadDrawable(Resources.java:3929) 
                      at android.content.res.Resources.getDrawable(Resources.java:1991) 
                      at android.content.res.Resources.getDrawable(Resources.java:1973) 
                      at android.content.Context.getDrawable(Context.java:409) 
                      at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:351) 
                      at jdev.com.handtwist.MainMenu.onCreate(MainMenu.java:63) 
                      at android.app.Activity.performCreate(Activity.java:6609) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:145) 
                      at android.app.ActivityThread.main(ActivityThread.java:7007) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                   Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #0: invalid drawable tag gradient
                      at android.content.res.ColorStateList.twCreateFromXmlInner(ColorStateList.java:176)
                      at android.content.res.ColorStateList.twCreateFromXml(ColorStateList.java:147)
                      at android.content.res.Resources.twLoadColorStateList(Resources.java:4175)
                      at android.content.res.Resources.loadColorStateList(Resources.java:4117) 
                      at android.content.res.TypedArray.getColor(TypedArray.java:397) 
                      at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1439) 
                      at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1418) 
                      at android.graphics.drawable.VectorDrawable.inflateInternal(VectorDrawable.java:544) 
                      at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:460) 
                      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1192) 
                      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1086) 
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:4045) 
                      at android.content.res.Resources.loadDrawable(Resources.java:3929) 
                      at android.content.res.Resources.getDrawable(Resources.java:1991) 
                      at android.content.res.Resources.getDrawable(Resources.java:1973) 
                      at android.content.Context.getDrawable(Context.java:409) 
                      at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:351) 
                      at jdev.com.handtwist.MainMenu.onCreate(MainMenu.java:63) 
                      at android.app.Activity.performCreate(Activity.java:6609) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275) 
                      at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:145) 
                      at android.app.ActivityThread.main(ActivityThread.java:7007) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

2 个答案:

答案 0 :(得分:0)

对于较旧的API级别,不存在对XML中的矢量绘图的本机支持。由于特殊ImageView属性,您可以在app:srcCompat中使用它们,但不能将它们用于android:background等。

但是,可以在Java中使用它们。像

这样的东西
Button b = findViewById(R.id.buttonSolo);
Drawable d = ContextCompat.getDrawable(this, R.drawable.ic_blue2);
b.setBackground(d);

应适用于所有API级别。

答案 1 :(得分:0)

从pastebin中的(更新)链接到您的资产,看起来您正在使用新的XML包格式。注意<aapt:attr name="android:fillColor">个节点。请参阅this G+ post中的Nick Butcher。即使它是关于动画矢量drawables,它适用于你的情况,因为我确信机制是相同的。

要检查的重点是:

  

这需要Build Tools 24+(目前在RC中),但输出是向后兼容的。

因此,可以检查您的构建工具是否已更新。