简体   繁体   English

Android Material - 无法投射到 com.google.android.material.chip.ChipGroup

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

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.我有一个component_category_view.xml ,在一个具有 id categoryList的constraintLayout 中初始化了一个 ChipGroup,我正在运行以下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)
    }
}


}

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:但是,当我运行并且我的代码到达它使用类别列表调用 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 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.company.project, PID: 8262 java.lang.ClassCastException: com.company.project.common.ui.CategoryView 不能转换为 com.google.android.material.chip。芯片集团

the component_category_view.xml looks like this 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>

Use Material theme instead of appCompact to solve chip problem:使用 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>

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)

}

And

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.就我而言,错误是由Chip视图中的id字段引起的。

<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.更改id字段的值后,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Material Chipgroup (Android) 中的动画芯片检查 - Animate Chip checking in Material Chipgroup (Android) 设置 com.google.android.material.chip.Chip 选择的颜色 - Set com.google.android.material.chip.Chip selected color 如何在com.google.android.material.chip.Chip上设置chipText? - How to set chipText on com.google.android.material.chip.Chip? MaterialTextView 不能转换为 com.google.android.material.textfield.TextInputEditText - MaterialTextView cannot be cast to com.google.android.material.textfield.TextInputEditText 无法更改 com.google.android.material.chip.Chip 更改新闻颜色 - Can't change com.google.android.material.chip.Chip change press color com.google.android.material.textview.MaterialTextView 无法转换为 ZC31B323614Text75CA8FCD150A。 - com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.EditText 使用Android Material Chip - Using Android Material Chip Android 材料芯片 - 带有 singleSelection 的芯片组无法动态添加芯片 - Android Material Chip - ChipGroup with singleSelection doesn't work adding chips dynamically 如何更改 Android Material ChipGroup 的背景颜色? - How to change background color of Android Material ChipGroup? 出现错误“androidx.appcompat.widget.N 无法转换为 com.google.android.material.button.MaterialButton” - Getting error "androidx.appcompat.widget.N cannot be cast to com.google.android.material.button.MaterialButton"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM