Material Chip引发应用崩溃:无法Inflate XML布局求助
解决
com.google.android.material.chip.Chip Inflate崩溃问题 嘿,我之前也碰到过类似的Chip组件初始化崩溃问题,结合你的情况,给你几个靠谱的修复方向:
先升级Material依赖版本
你用的1.0.0是Material组件的早期版本,里面确实存在Chip的一些初始化bug。建议直接升级到较新的稳定版,比如:implementation 'com.google.android.material:material:1.9.0'新版本修复了不少旧版的兼容性问题,大概率能解决这个Inflate异常。
确认主题是Material Components系列
Material组件(包括Chip)必须在Material主题下才能正常工作,不能用传统的AppCompat主题。检查你的styles.xml里的App主题:<!-- 正确的主题继承示例 --> <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- 你的主题配置项 --> </style>如果不想全局改主题,也可以在Chip标签里单独指定Material主题:
<com.google.android.material.chip.Chip android:id="@+id/chipActive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/Theme.MaterialComponents.Light" <!-- 其他属性 --> />调整XML属性的写法
旧版本Material组件对部分Android原生属性支持不好,比如你用的android:backgroundTint,可以换成Material专属的属性:app:chipBackgroundColor="@color/primaryColor"另外别忘了确认
@color/primaryColor和@color/primaryTextColor在colors.xml里都正确定义了,资源找不到也会间接导致Inflate失败。清理重建项目试试
有时候编译缓存搞鬼也会出现这种奇怪的崩溃,试试:- 点击Android Studio顶部的
Build->Clean Project - 再点
Build->Rebuild Project - 实在不行重启一下AS
- 点击Android Studio顶部的
内容的提问来源于stack exchange,提问作者pavlos
相关产品推荐
相关产品推荐

