You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

接入Android 12 Splash Screen API启动崩溃如何解决?

崩溃根因定位

你遇到的资源找不到异常由两个问题共同导致:

  1. 你使用的androidx.core:core-splashscreen:1.0.0-alpha02是早期测试版本,存在大量兼容层bug,不支持渐变Drawable作为启动页背景,会强制把背景资源解析为ColorStateList,解析失败就抛出NotFound异常。
  2. 你直接将渐变drawable@drawable/background_gradient赋值给了windowSplashScreenBackground属性,低版本系统下兼容层无法识别该类型资源。
修复步骤
    1. 升级SplashScreen依赖到稳定版
      替换Gradle中的依赖配置为最新稳定版:
implementation 'androidx.core:core-splashscreen:1.0.1'
    1. 分版本适配启动页背景
    • 新建values-v31/styles.xml(适配Android12及以上系统),保留你原有配置,原生系统支持渐变背景:
    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@drawable/background_gradient</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo_svg</item>
        <item name="postSplashScreenTheme">@style/Theme.AppNative</item>
    </style>
    
    • 在默认values/styles.xml中,将windowSplashScreenBackground改为纯色值,兼容低版本系统:
    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/你的渐变主色值</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo_svg</item>
        <item name="postSplashScreenTheme">@style/Theme.AppNative</item>
    </style>
    

    低版本的渐变效果可以在进入Activity后通过布局对齐实现,不影响视觉体验。

    1. 修正API调用顺序(可选,符合官方规范)
      将installSplashScreen()调用移到super.onCreate()之前,避免时序问题:
override fun onCreate(savedInstanceState: Bundle?) {
    installSplashScreen()
    super.onCreate(savedInstanceState)
    // 其余原有逻辑不变
}
    1. 清理缓存重打包
      执行Android Studio File → Invalidate Caches → Invalidate and Restart,清理旧构建缓存后重新编译,避免残留资源ID导致异常。

内容的提问来源于stack exchange,提问作者user2836797

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 13:27:06