我正在使用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,我通过value ->new resourceFile ->styles.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>
这是我的themes.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>
我Manifest.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>
我已经遵循了所有的解决方案,但没有一个适合我。我很感激你的努力。
最后,我找到了一个解决方案,实际上,我在Manifest.xml文件的应用程序标签中使用了我的应用程序主题这就是我所做的
<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">
现在这就是我在应用程序标签中应用我的应用程序主题而不是我的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()
当我试图将一个旧的View Activity集成到我的Jetpack Compose应用程序中时,我遇到了这个问题。
解决方案1
你可以在AndroidManifest.xml文件中设置View Activity的主题,像这样:
<activity
android:name=".screens.ChatActivity"
android:exported="true"
android:theme="@style/Theme.AppTheme" />
解决方案2
在你的Activity中使用setTheme切换到AppTheme来显示Activity,像这样:
class ChatActivity : AppCompatActivity() {
private lateinit var binding: ActivityChatBinding
override fun onCreate(savedInstanceState: Bundle?) {
// Switch to AppTheme for displaying the activity
setTheme(R.style.Theme_AppTheme)
super.onCreate(savedInstanceState)
binding = ActivityChatBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// ...
}
}
在你的themes.xml中试试这个
<style name="YourThemeName" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
</style>