2

我正在尝试使用新的 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.MaterialComponents.DayNight.NoActionBar"parent="Theme.AppCompat.DayNight.NoActionBar"但这并没有改变任何东西。

4

1 回答 1

2

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

@style/Theme.MyApp

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

@style/Theme.MyApp.Starting

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

于 2022-01-10T02:36:15.663 回答