[英]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.