使用矢量的 ShapeableImageView 未找到资源异常?
Resource not found exception for ShapeableImageView using vector?
我在 XML 中使用 ShapeableImageView 作为
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_unread_msg"
/>
当我在 OS 5 和 6
上 运行 应用程序时,我收到资源未找到异常
Caused by android.content.res.Resources$NotFoundException: File res/drawable/ic_unread_msg.xml from drawable resource ID #0x7f0700f7
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2850)
at android.content.res.Resources.loadDrawable(Resources.java:2732)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageView.<init>(ImageView.java:140)
at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:74)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:96)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:92)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
Caused by android.content.res.Resources$NotFoundException: File res/drawable/$ic_unread_msg__0.xml from color state list resource ID #0x7f07000b
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2960)
at android.content.res.Resources.loadColorStateList(Resources.java:2909)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1605)
at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1584)
at android.graphics.drawable.VectorDrawable.inflateInternal(VectorDrawable.java:666)
at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:571)
Caused by org.xmlpull.v1.XmlPullParserException: Binary XML file line #9: invalid color state list tag gradient
at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
at android.content.res.ColorStateList.createFromXml(ColorStateList.java:201)
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2956)
at android.content.res.Resources.loadColorStateList(Resources.java:2909)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
这是我的矢量图
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="14dp"
android:height="14dp"
android:viewportWidth="14"
android:viewportHeight="14">
<path android:pathData="M7,0L7,0A7,7 0,0 1,14 7L14,7A7,7 0,0 1,7 14L7,14A7,7 0,0 1,0 7L0,7A7,7 0,0 1,7 0z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="7"
android:endY="14"
android:startX="7"
android:startY="0"
android:type="linear">
<item
android:color="#FF63D8FF"
android:offset="0" />
<item
android:color="#FF6397FF"
android:offset="1" />
</gradient>
</aapt:attr>
</path>
</vector>
无法弄清楚为什么会发生崩溃?
感谢 Sandi 的评论
用 app:srcCompat
替换 android:src
有效
我在 XML 中使用 ShapeableImageView 作为
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_unread_msg"
/>
当我在 OS 5 和 6
上 运行 应用程序时,我收到资源未找到异常Caused by android.content.res.Resources$NotFoundException: File res/drawable/ic_unread_msg.xml from drawable resource ID #0x7f0700f7
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2850)
at android.content.res.Resources.loadDrawable(Resources.java:2732)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageView.<init>(ImageView.java:140)
at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:74)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:96)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:92)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
Caused by android.content.res.Resources$NotFoundException: File res/drawable/$ic_unread_msg__0.xml from color state list resource ID #0x7f07000b
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2960)
at android.content.res.Resources.loadColorStateList(Resources.java:2909)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1605)
at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1584)
at android.graphics.drawable.VectorDrawable.inflateInternal(VectorDrawable.java:666)
at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:571)
Caused by org.xmlpull.v1.XmlPullParserException: Binary XML file line #9: invalid color state list tag gradient
at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
at android.content.res.ColorStateList.createFromXml(ColorStateList.java:201)
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2956)
at android.content.res.Resources.loadColorStateList(Resources.java:2909)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
这是我的矢量图
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="14dp"
android:height="14dp"
android:viewportWidth="14"
android:viewportHeight="14">
<path android:pathData="M7,0L7,0A7,7 0,0 1,14 7L14,7A7,7 0,0 1,7 14L7,14A7,7 0,0 1,0 7L0,7A7,7 0,0 1,7 0z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="7"
android:endY="14"
android:startX="7"
android:startY="0"
android:type="linear">
<item
android:color="#FF63D8FF"
android:offset="0" />
<item
android:color="#FF6397FF"
android:offset="1" />
</gradient>
</aapt:attr>
</path>
</vector>
无法弄清楚为什么会发生崩溃?
感谢 Sandi 的评论
用 app:srcCompat
替换 android:src
有效