I want to create multiple View layout programmatically and add theme into another layout, in java do that was simple but in kotlin when i try that run time error happens!
my code is:
fun generateAnswersStatus(){
//@drawable/ui_top_rounded_answer_background_correct
for(q in match_game!!.getQuestions()!!){
val v = View(this)
v.id = q.getId()!!
v.layoutParams = ViewGroup.LayoutParams(30,40)
lytAnswerStatuses.addView(v)
if(q.getUser1Answer() == null){
v.setBackgroundResource(R.drawable.ui_top_rounded_answer_background_normal)
}else{
if(q.getUser1Answer()!!.toInt() == q.getCorrectAnswer()){
v.setBackgroundResource(R.drawable.ui_top_rounded_answer_background_correct)
}else{
v.setBackgroundResource(R.drawable.ui_top_rounded_answer_background_wrong)
}
}
lytAnswerStatuses.addView(v)
}
}
and error is:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
why?