【发布时间】: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