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

Kotlin应用运行后设备白屏问题求助

Kotlin应用白屏问题排查与解决

问题描述

开发Kotlin应用,目标实现点击按钮弹出弹窗功能,但运行后设备始终显示空白白屏,按钮在布局预览中可见但运行时不显示,尝试多种方案未解决。

相关代码

MainActivity代码

import android.app.AlertDialog
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.popup.R
import com.example.popup.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private var activityMainBinding: ActivityMainBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.lay_custom_dialog)
        title = "POP UP"
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        // add the following three lines
        activityMainBinding?.btnShowDefaultDialog?.setOnClickListener {
            showDefaultDialog()
        }
    }
    
    override fun onDestroy() {
        activityMainBinding = null
        super.onDestroy()
    }
    
    private fun showDefaultDialog() {
        val alertDialog = AlertDialog.Builder(this)
    
        alertDialog.apply {
            setIcon(R.drawable.ic_hello)
            setTitle("Hello")
            setMessage("I just wanted to greet you. I hope you are doing great!")
            setPositiveButton("Positive") { _, _ ->
                toast("clicked positive button")
            }
            setNegativeButton("Negative") { _, _ ->
                toast("clicked negative button")
            }
            setNeutralButton("Neutral") { _, _ ->
                toast("clicked neutral button")
            }
        }.create().show()
    }
    private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
    
    }

activity_main.xml布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/btnShowDefaultDialog"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/show_dialog"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.912" />
    
        <Button
            android:id="@+id/btnShowCustomDialog"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/show_custom_dialog"
            app:layout_constraintBottom_toTopOf="@id/btnShowDefaultDialog"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.238"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.977" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.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.Popup"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <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>

问题原因与解决方法

核心问题

MainActivity的onCreate方法中,先调用setContentView(R.layout.lay_custom_dialog)加载了空白布局(推测lay_custom_dialog本身无可见元素),后续调用DataBindingUtil.setContentView加载activity_main.xml时,布局已被之前的空白布局覆盖,导致运行时显示白屏。

修改方案

删除onCreate中的setContentView(R.layout.lay_custom_dialog)代码行,修改后的onCreate方法如下:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    title = "POP UP"
    activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    activityMainBinding?.btnShowDefaultDialog?.setOnClickListener {
        showDefaultDialog()
    }
}

额外检查

  • 确认lay_custom_dialog.xml是否为空白布局,若后续无需使用可直接删除,避免混淆。
  • 确保strings.xml中已定义show_dialog和show_custom_dialog文本资源,防止按钮文本不显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 11:25:28