首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MaterialAlertDialogBuilder崩溃并显示错误:当使用自定义视图时,指定的子节点已经具有父级。

MaterialAlertDialogBuilder崩溃并显示错误:当使用自定义视图时,指定的子节点已经具有父级。
EN

Stack Overflow用户
提问于 2021-12-05 16:45:51
回答 1查看 148关注 0票数 0

当在MaterialAlertDialog中使用自定义布局时,它会崩溃并给出以下错误:

代码语言:javascript
复制
    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

SettingsFragment:

代码语言:javascript
复制
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布局:

代码语言:javascript
复制
<?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>

这个错误有什么解决办法,为什么会发生?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-05 17:03:17

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70236429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档