You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin应用Splash Screen失效,仅显示白屏问题求助

启动页白屏问题解决

核心问题:未绑定Activity布局

你的Activity_Splash_Screen.kt中,虽然通过ViewBinding加载了布局文件,但没有调用setContentView()将布局挂载到Activity上,导致系统无法渲染启动页界面,只能显示默认的窗口白屏。

修改后的Activity_Splash_Screen.kt代码

package com.example.heytaksi

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.heytaksi.databinding.ActivitySplashScreenBinding

class Activity_Splash_Screen : AppCompatActivity() {
    lateinit var binding: ActivitySplashScreenBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySplashScreenBinding.inflate(layoutInflater)
        // 新增此行,将加载的布局设置为Activity的内容视图
        setContentView(binding.root)
        
        binding.image1.alpha = 0f
        binding.image1.animate().setDuration(1500).alpha(1f).withEndAction {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            finish()
        }
    }
}

额外优化建议

  • 删除无用代码:原代码中导入的java.util.zip.Inflater未被使用,建议删除以减少冗余。
  • 消除启动瞬间白屏:给启动页Activity单独配置主题,避免系统默认窗口背景的白屏闪烁。在AndroidManifest.xml中修改启动页Activity的配置:
<activity
    android:name=".Activity_Splash_Screen"
    android:exported="true"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

然后在res/values/styles.xml中添加自定义主题:

<style name="SplashTheme" parent="Theme.MaterialComponents.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
    <item name="android:statusBarColor">@color/black</item>
    <item name="android:navigationBarColor">@color/black</item>
</style>

其中@drawable/splash_background可以是包含启动图的drawable资源,让启动瞬间直接显示启动页背景。

  • 检查资源有效性:确认@drawable/anadol资源存在且格式正常(如PNG、JPG),资源缺失也会导致布局显示异常。

内容的提问来源于stack exchange,提问作者birisininkodarsivi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:40:30