android 无法启动活动ComponentInfo...您需要对此活动使用Theme.AppCompat主题(或后代

xqkwcwgp  于 2023-01-11  发布在  Android
关注(0)|答案(2)|浏览(118)

我正在使用SplashScreenApi实现SplashScreen,但当我运行应用程序时,出现闪屏,然后我的应用程序崩溃并显示此消息

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

这是我创建的splashTheme.xml,创建方法是:值-〉新资源文件-〉样式. xml

<resources>
    <style name="Theme.SplashTheme" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">#FFFFFF</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_baseline_baby_changing_station_24</item>
        <item name="windowSplashScreenAnimationDuration">200</item>
        <item name="postSplashScreenTheme">@style/Theme.SplashScreenAPI</item>
    </style>
</resources>

下面是我的主题. xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SplashScreenAPI" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

我的清单. xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SplashScreenAPI"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/Theme.SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

我已经遵循每一个解决方案,但没有为我工作。我会感谢你的努力。

mzsu5hc0

mzsu5hc01#

在themes.xml的父文件中尝试此操作

<style name="YourThemeName" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    
</style>
vof42yt1

vof42yt12#

最后,我找到了一个解决方案,实际上,我在Manifest.xml文件的application标记中使用了我的应用程序主题。

<application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SplashScreenAPI"
        tools:targetApi="31">

现在,我在application标记中应用我的app主题而不是splashScreen主题时,遇到了一个问题
问题:

android:theme="@style/Theme.SplashScreenAPI" //my app theme

解决方案:

android:theme="@style/Theme.SplashTheme"// my splash theme

然后在Manifest.xml的activity标签中设置应用主题。

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/Theme.SplashScreenApi">//my app theme>

另外,不要忘记在activity .java/kt文件中调用installSplashScreen()

相关问题