Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a
component_category_view.xml
with a ChipGroup initialized inside a constraintLayout that has an id
categoryList
,
I'm running the following
CategoryView.kt
class to inflate it and update it with chips dynamically and i have it placed inside my activity.
class CategoryView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
init {
inflateLayout()
private fun inflateLayout() {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.component_category_view, this, true)
fun updateCategories(categories: List<Category>){
categories.forEach {
var chipText = "${it.title.capitalize()} (${it.amount})"
val chip = Chip(this@CategoryView.context)
chip.text = chipText
chip.isCheckable = true
chip.chipBackgroundColor = null
categoryList.addView(chip)
When I run however and my code reaches the part where it's calling updateCategories with a list of Categories, the following error comes up:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.project, PID: 8262
java.lang.ClassCastException: com.company.project.common.ui.CategoryView cannot be cast to
com.google.android.material.chip.ChipGroup
the component_category_view.xml
looks like this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/layout_border_bottom">
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/categoryList"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Use Material theme instead of appCompact to solve chip problem:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Updated
The error occurred because you're adding chip to categoriesList but it's required a chip group like below example:
private fun createChip(name: String, index: Int){
val chip = Chip(chip_group.context)
chip.id = index
chip.text = name
chip.isClickable = true
chip.isCheckable = true
chip.isCheckedIconVisible = false
chip_group.addView(chip)
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
for ((index, item) in categories.withIndex()){
createChip(item.name, index)} }
–
–
–
In my case, the error was caused by the id
field within the Chip
view.
<com.google.android.material.chip.Chip
android:id="@+id/chip_fragment_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
After changing the value of the id
field, the error went away.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.