java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable
У меня есть такой код:
tvCategoryNameItem.setOnClickListener {
val color = (constraintLayout.background as ColorDrawable).color
btnRemove.visibility = View.VISIBLE
if (color == context.getColor(R.color.white)) {
selectedCategoryList.add(text)
constraintLayout.background = context.getDrawable(R.drawable.gradient_blue)
}
}
btnRemove.setOnClickListener {
selectedCategoryList.remove(text)
constraintLayout.setBackgroundColor(context.getColor(R.color.white))
btnRemove.visibility = View.GONE
}
Когда я нажимаю на кнопку один раз, кнопка как должна менят свой текст, но после второго нажатия выходит ошибка:
java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable
at logins.adapter.CategoryAdapter$CategoryHolder.addSelectedCategory(CategoryAdapter.kt:51)
строчка 51 это:
val color = (constraintLayout.background as ColorDrawable).color
Если я меняю строчку:
constraintLayout.background = context.getDrawable(R.drawable.gradient_blue)
на:
constraintLayout.setBackgroundColor(context.getColor(R.color.mycolor))
то все работает