添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
痴情的墨镜  ·  java.util.zip.ZipExcep ...·  1 月前    · 
从容的火锅  ·  webpack 不同source ...·  6 月前    · 
欢乐的熊猫  ·  java - What does JVM ...·  1 年前    · 
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)} }
                My manifest is already using material theme `            android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"`
– Omar Hussein
                Mar 5, 2019 at 11:47
                thanks for your help so far but categoryList is the name of my chip group, i've added the xml to my question so you can see it. so the only change in your answer is to set the context of the new chip (in my case) to categoryList.context, the same error occurs but in the line of creating the chip, setting it to just "context" makes the same error also occur but in the addition to chip group like in my question.
– Omar Hussein
                Mar 5, 2019 at 12:51
                If I use (val chip = Chip(context)) it occurs on line 71 (the addition line) If I use (val chip = Chip(categoryList.context)) it occurs on line 34 (the initialization line)
– Omar Hussein
                Mar 5, 2019 at 13:06

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.