【问题标题】:Android Material - cannot be cast to com.google.android.material.chip.ChipGroupAndroid Material - 无法转换为 com.google.android.material.chip.ChipGroup
【发布时间】:2019-07-26 20:29:15
【问题描述】:

我有一个component_category_view.xml,其中一个 ChipGroup 在一个约束布局内初始化,其 ID 为 categoryList, 我正在运行下面的 CategoryView.kt 类来给它充气并用芯片动态更新它,我把它放在我的活动中。

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: 致命异常: main 进程:com.company.project,PID:8262 java.lang.ClassCastException:com.company.project.common.ui.CategoryView 不能转换为 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>

【问题讨论】:

    标签: android kotlin material-design androidx


    【解决方案1】:

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

    【讨论】:

    • 我的清单已经在使用材质主题`android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"`
    • 感谢您迄今为止的帮助,但 categoryList 是我的芯片组的名称,我已将 xml 添加到我的问题中,以便您查看。所以你的答案中唯一的变化是将新芯片的上下文(在我的情况下)设置为 categoryList.context,同样的错误发生但是在创建芯片的行中,将它设置为“上下文”会产生同样的错误也会发生,但除了我的问题中的芯片组之外。
    • 如果我使用 (val chip = Chip(context)) 它出现在第 71 行(附加行)如果我使用 (val chip = Chip(categoryList.context)) 它出现在第 34 行(初始化行)
    • 那么你使用 categoryList.context 和哪一行是 34
    • 当然线芯片作为View表示芯片是CategoryView。只使用芯片
    【解决方案2】:

    在我的例子中,错误是由 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字段的值后,错误消失了。

    【讨论】:

      猜你喜欢
      • 2021-01-04
      • 2023-04-03
      • 2021-09-12
      • 2013-02-22
      • 1970-01-01
      • 2013-09-17
      • 2013-12-05
      • 1970-01-01
      相关资源
      最近更新 更多