使用新的Android 12 Splash Screen API应用程序崩溃



我正在尝试使用新的Android 12 Splash Screen API,但我的应用程序在打开第一个活动时一直崩溃。

我有MainActivity作为我的启动器活动,没有任何与其相关的布局文件。当应用程序启动时,我会在检查当前身份验证会话时保持启动屏幕处于活动状态。

// in MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val splashScreen = installSplashScreen()
splashScreen.setKeepVisibleCondition { !authSessionIsReady }
Amplify.Auth.fetchAuthSession(onFetchSuccess, onFetchError)
}
private val onFetchSuccess = fun(session: AuthSession) {
authSessionIsReady = true
when (session.isSignedIn) {
true -> goToHomeActivity(Amplify.Auth.currentUser.username)
false -> goToLoginOrSignupActivity()
}
}
private val goToHomeActivity = fun(username: String) {
Intent(this, HomeActivity::class.java).apply {
putExtra(EXTRA_USERNAME, username)
}.also { startActivity(it) }
finish()
}

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.myapp">
<application
android:name=".AmplifyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp.Starting">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginOrSignupActivity"
android:exported="false" />
<activity
android:name=".HomeActivity"
android:exported="false" />
</application>
</manifest>

这是我使用的主题文件

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Splash screen theme. -->
<style name="Theme.MyApp.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/black</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
<item name="postSplashScreenTheme">@style/Theme.MyApp</item>
</style>
<!-- Base application theme. -->
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Customize your theme here. -->
</style>
</resources>

一旦验证结果返回,应用程序就会崩溃,并出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.LoginOrSignupActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

由于错误似乎是You need to use a Theme.AppCompat theme (or descendant) with this activity,我尝试在主题文件中用parent="Theme.AppCompat.DayNight.NoActionBar"替换parent="Theme.MaterialComponents.DayNight.NoActionBar",但这并没有改变任何内容。

将应用程序标记中的主题更改为

@style/Theme.MyApp

并在活动标签中添加主题(您的主要活动(到

@style/Theme.MyApp.Starting

让我知道这是否适合你。例如,可以检查我的应用的清单文件

建议的解决方案对我不起作用。对我来说,罪魁祸首是一些androidx库的最新版本。我做了一些测试,以下是对我有效的和不适用的。请记住,稳定版本和当前最新版本之间的版本可能会也可能不会导致这个问题,但我不会测试所有版本。

以下是给我带来问题的库/工件

androidx.navigation

androidx.navigation:navigation-fragment-ktx:$version
androidx.navigation:navigation-ui-ktx:$version

(当前为写入时间(稳定2.4.2 -> works

(当前(最新:2.5.0-beta01 -> causes the issue

2.5.0-alpha03是最新的没有给我带来问题的。

androidx.fragment

androidx.fragment:fragment-ktx:$version

稳定1.4.1 -> works

最新1.5.0-beta01 -> causes the issue

1.5.0-alpha03是最新的没有给我带来问题的。

我遇到了同样的问题,如果对某人有用:我修复了它设置(在清单文件中(防溅屏主题,仅用于将使用防溅屏的活动(而不是整个应用程序(,对于这种特殊情况,它将是:

<application
android:name=".AmplifyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.MyApp.Starting">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginOrSignupActivity"
android:exported="false" />
<activity
android:name=".HomeActivity"
android:exported="false" />
</application>

最新更新