Resources$NotFoundException 启动 SplashScreen 时

Resources$NotFoundException when launching SplashScreen

我的应用程序有一个作为单独 activity 实现的 SplashScreen。

我在 Google Play Console 上收到的崩溃报告很少。这是堆栈跟踪:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3253)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3349)
  at android.app.ActivityThread.access00 (ActivityThread.java:223)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1794)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:148)
  at android.app.ActivityThread.main (ActivityThread.java:7223)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
Caused by: android.content.res.Resources$NotFoundException: 
  at android.content.res.Resources.getValue (Resources.java:2558)
  at androidx.appcompat.widget.ResourceManagerInternal.loadDrawableFromDelegates (ResourceManagerInternal.java:252)
  at androidx.appcompat.widget.ResourceManagerInternal.getDrawable (ResourceManagerInternal.java:139)
  at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:411)
  at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown (TintTypedArray.java:93)
  at androidx.appcompat.app.AppCompatDelegateImpl.attachToWindow (AppCompatDelegateImpl.java:794)
  at androidx.appcompat.app.AppCompatDelegateImpl.ensureWindow (AppCompatDelegateImpl.java:770)
  at androidx.appcompat.app.AppCompatDelegateImpl.onCreate (AppCompatDelegateImpl.java:498)
  at androidx.appcompat.app.AppCompatActivity.onCreate (AppCompatActivity.java:114)
  at my.application.splashscreen.view.Hilt_SplashActivity.onCreate (Hilt_SplashActivity.java:37)
  at my.application.splashscreen.view.SplashActivity.onCreate (SplashActivity.kt:16)
  at android.app.Activity.performCreate (Activity.java:6877)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1136)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3206)

以下是我的 Activity 在应用程序中的实现方式:

AndroidManifest.xml

<activity
        android:name="my.application.splashscreen.view.SplashActivity"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

SplashActivity.kt

@AndroidEntryPoint
class SplashActivity : AppCompatActivity() {
    private val splashViewModel: SplashViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (splashViewModel.isUserLoggedIn())
            proceedToProfileScreen(this)
        else
            proceedToLoginScreen(this)
    }
}

/res/values/styles.xml -> SplashTheme

<style name="SplashTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/networkPrimary</item>
    <item name="colorPrimaryDark">@color/networkPrimaryDark</item>
    <item name="colorAccent">@color/networkFluorescentBlue</item>
    <item name="android:windowBackground">@drawable/bg_splash_screen</item>
</style>

/res/drawable/bg_splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/networkPrimary" />
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/logo" />
    </item>
</layer-list>

一切正常,但在某些设备上仍然很少发生崩溃。也许有人可以帮我解决这个问题。

编辑:根据 Google 崩溃和 ANR 页面 - 这个特定问题似乎只影响 Android 6.0 (SDK 23)。 没有其他手机 Android 版本受此影响。

此问题与 drawable 个文件夹有关。将所有可绘制对象的副本添加到 drawable 文件夹,您将解决问题。