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
相关产品推荐
相关产品推荐

