当在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 64SettingsFragment:
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>这个错误有什么解决办法,为什么会发生?
发布于 2021-12-05 17:03:17
我找到了解决方案,我必须将valueBinding.root传递给setView()而不是valueBinding.editValue,,因为TextInputEditText附在ConstraintLayout,上,感谢Mike的帮助。
https://stackoverflow.com/questions/70236429
复制相似问题