【问题标题】:Kotlin dialog java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent firstKotlin 对话框 java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
【发布时间】:2018-12-03 11:01:55
【问题描述】:

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

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

E/AndroidRuntime: 致命异常: main
java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。

我在网上看到了一些 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() }
    }
}

【问题讨论】:

    标签: android kotlin android-alertdialog illegalstateexception


    【解决方案1】:

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

    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() }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 2013-10-18
      相关资源
      最近更新 更多