第二次单击时自定义 AlertDialog 错误

Custom AlertDialog error when click second time

这是代码:

class Widgets_TextView : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.widgets_textview)

        val inflater = layoutInflater
        val dialogView = inflater.inflate(R.layout.customed_dialog, null)

        widgets_textview_mainkt.setOnClickListener(View.OnClickListener {
            dialogView.findViewById<TextView>(R.id.main_content).setText("AAAA")
            val alertDialog = AlertDialog.Builder(this).setView(dialogView)
            alertDialog.show()

        })
    }
}

第一次点击还好,第二次点击就报错

这是错误:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我该怎么办?

只需在setView的末尾添加create()即可。您将能够一次又一次地使用相同的对话框。

class Widgets_TextView : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.widgets_textview)

            val inflater = layoutInflater
            val dialogView = inflater.inflate(R.layout.customed_dialog, null)
            val alertDialog = AlertDialog.Builder(this).setView(dialogView).create()
            widgets_textview_mainkt.setOnClickListener(View.OnClickListener {
                dialogView.findViewById<TextView>(R.id.main_content).setText("AAAA")
                alertDialog.show()
            })
        }
    }

您可以创建一个函数 ShowAlertDailog 并在您的 onCreate 中调用此函数 Activity。

查看下面的ShowAlertDailog代码

private fun showFilterDailog() {
    val layoutInflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val layoutInflateView = layoutInflater.inflate(R.layout.filter_dialog_item, null)
    val builder = AlertDialog.Builder(context)
    builder.setView(layoutInflateView)
    mdialog = builder.create()



    var alertclear: TextView
    var alertcancel: TextView
    var txt_header: TextView



    txt_header = layoutInflateView.findViewById(R.id.txt_header)
    alertcancel = layoutInflateView.findViewById(R.id.alertcancel)
    alertclear = layoutInflateView.findViewById(R.id.alertclear)



    alertok.setOnClickListener(object : View.OnClickListener {
        override fun onClick(view: View?) {
            if (mdialog != null) {
                mdialog?.dismiss()
            }
         // This Helps you to close dailogbox if it is alredy open and 
         // Then after your can write your code to perform as per your requirment.
        }
    })
    alertdismiss.setOnClickListener(object : View.OnClickListener {
        override fun onClick(view: View?) {

            mdialog?.dismiss()
          // This Helps you to close dailogbox 
        }
    })

    mdialog!!.window!!.setBackgroundDrawableResource(android.R.color.transparent)
    mdialog!!.setCancelable(false)
   // With the help of setcancelable=false your dialog is not close
    // if some touch on  phone.
    mdialog!!.show()

}