Kotlin 对话框 java.lang.IllegalStateException: 指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()

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

一旦用户在我的 activity 上单击 change_account,就会显示一个对话框,然后一旦用户单击此对话框上的创建计数,我想显示另一个对话框。

但不幸的是我得到了这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我在网上看到一些 removeView() 的代码,但我不知道如何使用它。特别是因为我从另一个对话框调用对话。

这是我的代码,导致错误的行是

 creatCount.create().apply { show() }

这里是完整的代码:

class ClientAcountActivity : AppCompatActivity(),AdapterView.OnItemClickListener{

    override fun onCreate(savedInstanceState: Bundle?) {

        ....

        change_account.setOnClickListener { openChangeCompte() }

    }


    fun openChangeCompte()
    {
        val dialogBuilder = AlertDialog.Builder(this)
        val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val row = inflater.inflate(R.layout.dialog_listview, null, false)
        val listAccount= row.findViewById<ListView>(R.id.transfer_type_list)
        Log.d("ClientAccountActivity", Injection.provideAccountRepository().availableAccountsType.toString())
        listAccount.adapter = CountChangeAdapter(Injection.provideAccountRepository().availableAccountsType, this)

       listAccount.onItemClickListener = AdapterView.OnItemClickListener { adapterView: AdapterView<*>, view: View, i: Int, id: Long ->

           if((adapterView.getCount()!=4) && (i==adapterView.getCount()-1))
                {
                     val creatCount: AlertDialog.Builder = AlertDialog.Builder(this).apply {
                           setView(row)
                           setTitle("Quel compte voulez vous créer ")
                           setPositiveButton("OK",  DialogInterface.OnClickListener() {
                               dialogInterface: DialogInterface, i: Int ->
                               fun onClick(dialog:DialogInterface , which:Int) {
                           }})
                           setNegativeButton("Cancel",  DialogInterface.OnClickListener() {
                               dialogInterface: DialogInterface, i: Int ->
                               fun  onClick(dialog:DialogInterface ,  which:Int) {
                                   finish()
                           }})
                       }
                    creatCount.create().apply { show() } //the line which cause the pb 

                }
                else
                {
                    Injection.provideAccountRepository().selectedAccount=id.toInt()
                    updateBalance()
                    changeAccoutDialog!!.dismiss()
                }
            }
        dialogBuilder.setView(row)
        dialogBuilder.setTitle("Quel compte voulez vous choisir?")
        changeAccoutDialog = dialogBuilder.create().apply { show() }
    }
}

这里的问题是由于将变量 row 设置为两个对话框的视图:您应该创建第二行(使用 inflater 具有相同的布局)以便将其设置为第二个对话框。这是更正后的代码:

fun openChangeCompte()
{
    val dialogBuilder = AlertDialog.Builder(this)
    val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val row = inflater.inflate(R.layout.dialog_listview, null)
    val listAccount= row.findViewById<ListView>(R.id.transfer_type_list)
    Log.d("ClientAccountActivity", Injection.provideAccountRepository().availableAccountsType.toString())
    listAccount.adapter = CountChangeAdapter(Injection.provideAccountRepository().availableAccountsType, this)

    listAccount.onItemClickListener = AdapterView.OnItemClickListener { adapterView: AdapterView<*>, view: View, i: Int, id: Long ->

        if((adapterView.count !=4) && (i==adapterView.count -1))
        {
            val row2 = inflater.inflate(R.layout.dialog_listview, null)
            val listAccount= row2.findViewById<ListView>(R.id.transfer_type_list)
            val creatCount: AlertDialog.Builder = AlertDialog.Builder(this).apply {
                setView(row2)
                setTitle("Quel compte voulez vous créer ")
                setPositiveButton("OK") { _: DialogInterface, _: Int ->
                    fun onClick(dialog:DialogInterface , which:Int) {
                    }}
                setNegativeButton("Cancel") { _: DialogInterface, _: Int ->
                    fun  onClick(dialog: DialogInterface, which:Int) {
                        finish()
                    }}
            }
            creatCount.create().apply { show() } //the line wich cause the pb

        }
        else
        {
            Injection.provideAccountRepository().selectedAccount=id.toInt()
            updateBalance()
            changeAccoutDialog !!.dismiss()
        }
    }
    dialogBuilder.setView(row)
    dialogBuilder.setTitle("Quel compte voulez vous choisir?")
    changeAccoutDialog = dialogBuilder.create().apply { show() }
}