MaterialAlertDialogBuilder crashes and shows error: The specified child already has a parent, when using custom view

MaterialAlertDialogBuilder crashes and shows error: The specified child already has a parent, when using custom view

当使用带有 MaterialAlertDialog 的自定义布局时,它崩溃并给我这个错误:

    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at com.example.applux.SettingsFragment.onViewCreated$lambda-3(SettingsFragment.kt:64) // line 64

设置片段:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = FragmentSettingsBinding.bind(view)

        binding.settingsClickUsername.setOnClickListener {
            val valueBinding = EditValuesBinding.inflate(layoutInflater)
            MaterialAlertDialogBuilder(requireContext())
                .setTitle("Edit your username")
                .setView(valueBinding.editValue)
                .setPositiveButton("Save"){ dialog, p ->

                }
                .setNegativeButton("Cancel"){ dialog, p ->

                }
                .show() // this is the line 64
        }
}

edit_values.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/edit_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

此错误的任何解决方案及其发生原因?

我找到了解决方案,我必须将 valueBinding.root 传递给 setView() 而不是 valueBinding.editValue,因为 TextInputEditText 附加到 ConstraintLayout,感谢 Mike M 的帮助。