问题描述
当我试图在下面的行中获取九个可绘制的补丁时,我遇到ResourcesNotFoundException。我遇到此问题的设备是LGE Nexus(Android 8)和Google Pixel(Android 10)。
mRecyclerViewDragDropManager.setDraggingItemShadowDrawable(
(NinePatchDrawable) ContextCompat.getDrawable(this,R.drawable.material_shadow_z3));
例外
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo: android.content.res.Resources$NotFoundException: Resource ID #0x7f0800af
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by android.content.res.Resources$NotFoundException: Resource ID
#0x7f0800af
at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:204)
at android.content.res.Resources.getDrawableForDensity(Resources.java:875)
at android.content.res.Resources.getDrawable(Resources.java:818)
at android.content.Context.getDrawable(Context.java:605)
at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:463)
at init(ImagesActivity.java:211)
at onCreate(ImagesActivity.java:121)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
我已经在drawable-hdpi,drawable-mdpi,drawable-xhdpi,drawable-xxhdpi,drawable-xxhdpi中提供了material_shadow_z3。就像-
material_shadow_z3.xml
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:src="@drawable/material_shadow_z3_mdpi"
tools:ignore="unused" />
根据评论进行更新-
material_shadow_z3_mdpi
位于drawable-nodpi文件夹中,是一个9补丁的png图像。类似地,我material_shadow_z3_xhdpi,material_shadow_z3xxhdpi,material_shadow_z3_xxxhdpi
位于同一个drawable-nodpi文件夹中。
解决方法
不能100%确定是什么原因引起的,但是我的猜测是文件的排序方式存在问题,尽管您可以将所有文件用于本地版本,但由于这些原因,它们在用户的安装中可能不可用如果您使用的是应用捆绑包,则可以获取针对其特定设备的优化apk。
根据the resource documentation和the nine patch guide,这是通常放置资源的方式。
选项1-单张9补丁图像
如果您有一个9patch图像,可以缩放和拉伸该图像以任何密度使用,只需将.9.png
放在drawable
文件夹中。
选项2-不同密度的9张补丁图像
如果9patch的某些方面需要根据密度而有所不同,则可以在每个相关的.9.png
文件夹中放置一张drawable-<density>
图像。如果未提供,则系统将在运行时加载相关图像,或从其他密度缩放最合适的图像。
仅当您要手动设置抖动时,才使用XML资源声明。
因此,对于您的用例:删除material_shadow_z3.xml
并将material_shadow_z3<-xxx>.9.png
放在没有密度标识符的适当可绘制文件夹中。如果图像本身不是真的依赖于密度(例如带有阴影边缘的灰色条),则可以只使用一个9patch png更好。