ResourcesNotFoundException 资源可用时(API 22 和 23 台设备)

ResourcesNotFoundException when resource available (API 22 and 23 devices)

我只在 API 22 和 23 台设备上遇到此崩溃。

(android.content.res.Resources$NotFoundException: File res/drawable/show_empty_state.xml from drawable resource ID #0x7f080156 at android.content.res.Resources.loadDrawableForCookie(Resources.java:2640) at android.content.res.Resources.loadDrawable(Resources.java:2540) at android.content.res.Resources.getDrawable(Resources.java:806) at android.content.Context.getDrawable(Context.java:458) at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:358) at com.myProj.utils.EmptyStateView.setImage(EmptyStateView.java:55) at com.myProj.tab_two.ShowFragment.onCreateView(ShowFragment.java:111) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)

资源确实存在,并且在更高的设备上运行良好。我最近所做的唯一更改是使用 SVG 为 show_empty_state 替换了一个新的矢量图像。 我确实看过其他一些相关问题。到目前为止找不到答案。任何帮助表示赞赏。谢谢

您需要确保在矢量可绘制对象的路径标记内使用填充颜色作为十六进制代码,而不是任何对颜色文件的引用。

例如:

你需要使用

android:fillColor="#FFFFaa"

而不是

android:fillColor="@color/colorname"

崩溃是因为矢量资产有 <24 台设备的 gradient 标签。

android:fillColor
Specifies the color used to fill the path. May be a color or, for SDK 24+, a color state list or a gradient color (See GradientColor and GradientColorItem). If this property is animated, any value set by the animation will override the original value. No path fill is drawn if this property is not specified.

替换

<gradient android:endX="45300.0" android:endY="-86698.0"
       <android:startX="45300.0" android:startY="-3118.0" android:type="linear">
       <item android:color="#FF1AA186" android:offset="0.0"/>
       <item android:color="#FF23B899" android:offset="1.0"/>
 </gradient>

android:fillColor="#FF1AA186"

修复它。

更好的办法是用 <24 sdk 的非渐变资产替换资产。