0

I am trying to add chips into ChipGroup(not singleLine):

val chip = Chip(this)
    chip.isCloseIconVisible = true
    for (i in 0..10) {
        chip.setText("Some text $i")
        chip_group.addView(chip as View)
    }

But I get an exception:

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

How can I mark chip as a unique child? Or what should I do?

Skullper
  • 714
  • 5
  • 22

2 Answers2

1

You need to declare chip inside for loop

for (i in 0..10) {
    val chip = Chip(this)
    chip.isCloseIconVisible = true
    chip.setText("Some text $i")
    chip_group.addView(chip as View)
}

Since you declare it as a val (which means not changed it is value), you are getting the same child error.

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
0

You cant add the same view more than once to parent.

Refer this : https://stackoverflow.com/a/24032857

It throws the above mentioned exception.

You are trying to add the same Chip to the parent more than once.

Try modified code like,

    for (i in 0..10) {
        val chip = Chip(this)
        chip.isCloseIconVisible = true
        chip.setText("Some text $i")
        chip_group.addView(chip as View)
    }
Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29