BottomNavigationView加载报错需Theme.AppCompat主题仅部分用户崩溃问题咨询
根因分析
核心报错The style on this component requires your app theme to be Theme.AppCompat (or a descendant)触发的原因有两点,均为兼容类问题所以仅在部分设备上复现:
- 颜色状态选择器存放路径错误:
app:itemIconTint和app:itemTextColor属性要求传入颜色类型资源,你把bottom_nav_colours.xml放在drawable目录下,低版本Android系统无法正确识别drawable目录下的颜色选择器,会导致控件初始化读取属性失败,触发主题校验报错 - 上下文主题丢失:部分设备系统级主题覆盖、Activity配置变更(横竖屏切换、深色模式切换)场景下,Activity上下文临时丢失正确主题配置,触发ThemeEnforcement校验失败
修复方案
1. 修正颜色选择器路径
把bottom_nav_colours.xml从drawable/drawable-any-dpi-v24目录移动到res/color目录下,没有color目录手动创建即可。
修改布局中对应属性的引用路径:
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="@color/nav_bar" app:labelVisibilityMode="labeled" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/navigation" app:itemIconTint="@color/bottom_nav_colours" app:itemTextColor="@color/bottom_nav_colours" />
2. 显式指定控件主题
在BottomNavigationView节点添加android:theme属性,强制绑定符合要求的主题,避免上下文主题丢失:
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar.Bridge"
3. 校验Activity主题配置
检查AndroidManifest.xml中MainActivity的android:theme属性,确认没有单独配置不符合要求的主题,需和应用级主题保持一致,均继承AppCompat/MaterialComponents Bridge系列主题。
4. 排查动态主题切换逻辑
如果你的应用支持动态切换主题、深色模式,检查切换逻辑是否在Activity重建前生效,避免重建时读取到错误的主题配置。
5. 可选升级依赖
如果以上操作后仍有崩溃,可以升级Google Material组件库到最新稳定版,规避旧版本ThemeEnforcement校验逻辑的已知兼容问题。
内容的提问来源于stack exchange,提问作者SmallGrammer
相关产品推荐
相关产品推荐

