Android Material - 无法转换为 com.google.android.material.chip.ChipGroup

Android Material - cannot be cast to com.google.android.material.chip.ChipGroup

我有一个 component_category_view.xml,其 ChipGroup 在具有 ID categoryList 的 constraintLayout 中初始化, 我正在 运行 宁以下 CategoryView.kt class 来给它充气并用芯片动态更新它,我把它放在我的 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)
    }
}


}

然而,当我 运行 并且我的代码到达使用类别列表调用 updateCategories 的部分时,出现以下错误:

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

component_category_view.xml 看起来像这样

<?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>

使用Material主题代替appCompact来解决芯片问题:

<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>

已更新

发生错误是因为您正在向 categoriesList 添加芯片,但它需要一个芯片组,如下例所示:

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)} }

在我的例子中,错误是由 Chip 视图中的 id 字段引起的。

<com.google.android.material.chip.Chip
                android:id="@+id/chip_fragment_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

更改 id 字段的值后,错误消失了。