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

App启动时触发WindowLeaked错误致崩溃,求解决方案

Android WindowLeaked 错误排查求助

首次启动App时需显示对话框询问用户是否需要教程,但触发以下错误:

android.view.WindowLeaked: Activity com.example.mypackage has leaked window DecorView@43aa395[Tutorial] that was originally added here ...

查阅资料得知该错误可能因对话框打开时切换Activity导致,但我的场景中仅点击对话框内按钮才会切换Activity。相关代码如下:

val tut = getSharedPreferences("tutorial", MODE_PRIVATE)
val dialog = Dialog(this@MainActivity)

if (tut.getBoolean("my_first_time", true)) { //if the app is being launched for first time, do something

    dialog.setContentView(R.layout.tutorial_dial) //sets xml tag
    dialog.setTitle("Tutorial") //idk what this does
    dialog.setCancelable(true) //this... confuses me, my guess is that it allows you to click off the dialogue box
    val button = dialog.findViewById<View>(R.id.yeah) as Button
    button.setOnClickListener {
        val intent = Intent(this, Tutorial1::class.java)
        dialog.dismiss()
        startActivity(intent)
    }
    val button2 = dialog.findViewById<View>(R.id.nahhhh) as Button
    button2.setOnClickListener {
        dialog.dismiss()
    }

    dialog.show()

    tut.edit().putBoolean("my_first_time", false).apply() // record the fact that the app has been started at least once
}else{dialog.dismiss()}

经排查,错误由dialog.show()行触发,恳请各位提供帮助。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 08:05:23