Android 11及以下设备启动SplashActivity遇资源未找到异常求助
安卓11及以下启动屏崩溃问题分析
仅在Android 11及以下设备中出现SplashActivity启动崩溃,堆栈跟踪显示找不到background_splash资源,但该文件实际存在。已通过Android 12+启动屏迁移方案解决高版本问题,需排查低版本根因。
崩溃堆栈
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{..SplashActivity}: android.content.res.Resources$NotFoundException: Drawable:drawable/background_splash with resource ID #0x7f0800cb Caused by android.content.res.Resources$NotFoundException: Drawable:drawable/background_splash with resource ID #0x7f0800cb Caused by android.content.res.Resources$NotFoundException: File res/drawable/background_splash.xml from drawable resource ID #0x7f0800cb at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:898) at android.content.res.ResourcesImpl.originalLoadDrawable(ResourcesImpl.java:679) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:586) at android.content.res.MiuiResourcesImpl.loadDrawable(MiuiResourcesImpl.java:307) at android.content.res.Resources.loadDrawable(Resources.java:932) at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:1007) at android.content.res.TypedArray.getDrawable(TypedArray.java:982) Caused by android.content.res.Resources$NotFoundException: Resource ID #0x7f08090e at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:248) at android.content.res.Resources.getValueForDensity(Resources.java:1385) at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:819)
相关资源代码
background_splash.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/white"/> <item android:bottom="128dp" android:gravity="center_horizontal|center_vertical|center" android:drawable="@drawable/splash_logo_p4b_new"> </item> <item android:gravity="bottom|fill_horizontal" android:drawable="@drawable/blue_bottom_strip"> </item> </layer-list>
其中blue_bottom_strip和splash_logo_p4b_new为PNG格式图片。
启动屏主题样式
<style name="ThemeSplashScreen" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/color_white</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@drawable/background_splash</item> <item name="android:windowContentOverlay">@null</item> <item name="android:backgroundDimEnabled">false</item> </style>
AndroidManifest配置
<activity android:name=".splash.SplashActivity" android:launchMode="singleTask" android:screenOrientation="portrait" android:theme="@style/ThemeSplashScreen" android:exported="true">
根因分析
从堆栈深层报错Caused by android.content.res.Resources$NotFoundException: Resource ID #0x7f08090e可以看出,真正找不到的不是background_splash.xml本身,而是它引用的某个子资源(splash_logo_p4b_new或blue_bottom_strip),可能的原因包括:
- 图片资源仅存放在高版本drawable目录(如
drawable-v24),Android 11及以下设备无法访问 - 资源文件名存在大小写问题,低版本系统对资源文件名大小写敏感
- 资源压缩(R8/ProGuard)时未保留这些图片资源,导致打包后被移除
- MIUI系统兼容性问题:堆栈中出现
MiuiResourcesImpl,部分MIUI版本对layer-list引用的图片加载有特殊处理逻辑,引发加载失败
解决方案建议
- 将
blue_bottom_strip和splash_logo_p4b_new复制到通用的drawable目录,确保低版本设备能访问到 - 检查资源压缩规则,在
proguard-rules.pro中添加保留规则:-keepresources drawable/splash_logo_p4b_new* -keepresources drawable/blue_bottom_strip* - 如果
blue_bottom_strip是纯色条,可将其替换为shape drawable,避免图片加载问题 - 避免在主题中直接设置
android:background,改为在SplashActivity的布局文件中设置背景,延迟资源加载时机
内容的提问来源于stack exchange,提问作者Vikas Pandey
相关产品推荐
相关产品推荐

