简体   繁体   English

如何在孩子的父母上调用 removeView

[英]How to call removeView on the child's parent

I'm creating a custom dialog, but when the user opens it for the second time, I get this exception: The specified child already has a parent.我正在创建一个自定义对话框,但是当用户第二次打开它时,我收到了这个异常:指定的孩子已经有一个父母。 You must call removeView() on the child's parent first您必须先在孩子的父母上调用 removeView()

I tried to call removeView on mDialog but I cannot.我试图在 mDialog 上调用 removeView 但我不能。 This error is common so I tried to implement solutions on other posts;这个错误很常见,所以我尝试在其他帖子上实施解决方案; when inflating mCustomDatePicker, I tried to set the root (in the constructor) as the parent of the inflated view, but it didn't help.在膨胀 mCustomDatePicker 时,我尝试将根(在构造函数中)设置为膨胀视图的父级,但没有帮助。

class SignupStageOne : AppCompatActivity() {

    private var mCalendar : Calendar? = null
    private var day: Int? = null
    private var month: Int? = null
    private var year: Int? = null

    private var age: String? = null

    private val firebaseDatabase = FirebaseDatabase.getInstance().reference
    private val firebaseAuth = FirebaseAuth.getInstance()
    val user = firebaseAuth.currentUser

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.example.meet.R.layout.activity_signup_stage_one)

        val mCustomDatePicker = layoutInflater.inflate(com.example.meet.R.layout.custom_date_picker, datePickerLayout, false)

        var mDatePicker = mCustomDatePicker.findViewById(com.example.meet.R.id.mDatePicker) as DatePicker
        mDatePicker.maxDate = (Calendar.getInstance().getTime().getTime())

        val mDialog = AlertDialog.Builder(this)
        mDialog.setView(mCustomDatePicker)

        mDialog.setPositiveButton("OK", object : DialogInterface.OnClickListener {
            override fun onClick( dialog: DialogInterface, which: Int) {

            }
        }).setNegativeButton("Cancel", object: DialogInterface.OnClickListener {
            @Override
            override fun onClick( dialog: DialogInterface, which: Int) {
                dialog.dismiss()
            }
        })

        mDialog.create()

        ageET.setOnFocusChangeListener{ view, hasFocus ->
            if (hasFocus) {
                mDialog.show()
            }
        }
    }

I also tried to inflate the view like so:我还尝试像这样夸大视图:

layoutInflater.inflate(com.example.meet.R.layout.custom_date_picker, null, false)

But it didn't help.但它没有帮助。

My XML:我的 XML:

<RelativeLayout
    android:id="@+id/datePickerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <DatePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mDatePicker"
        android:layout_gravity="center_horizontal"
        android:spinnersShown="true"
        android:calendarViewShown="false"
        android:layout_alignParentTop="true"/>
</RelativeLayout>

My lack of knowledge in Kotlin prevented me from solving this, when accessing the parent of mCustomDatePicker , we have to cast it as ViewGroup:我对 Kotlin 缺乏了解使我无法解决这个问题,在访问mCustomDatePicker的父mCustomDatePicker ,我们必须将其转换为 ViewGroup:

override fun onClick( dialog: DialogInterface, which: Int) {
    ageET.clearFocus()
    val parent = mCustomDatePicker.parent as ViewGroup?
    parent?.removeAllViews()
    dialog.dismiss()
}

That solved the problem.那解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM