项目中原先所使用的AppTheme继承自Theme.MaterialComponents.Light.NoActionBar
由于一些原因需要替换为Theme.AppCompat.Light.NoActionBar
在替换之后发现在打开带有com.google.android.material.tabs.TabLayout控件的页面时会发生崩溃 查看日志后发现出现了以下log
Error inflating class com.google.android.material.tabs.TabLayout
Attempt to invoke virtual method 'int android.content.res.ColorStateList.getDefaultColor()' on a null object reference
一开始以为按照网上的方法改了控价的android:theme属性 但是不生效
还没才发现这个控件来自material库 应该使用material的theme
最终的修复结果
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:theme="@style/AppTheme.Main"
android:layout_width="match_parent"
android:layout_height="31dp"
android:layout_marginTop="21dp"
app:tabIndicator="@drawable/indicator_search_tab"
app:tabIndicatorColor="#E03224"
app:tabIndicatorFullWidth="false"
app:tabMode="scrollable"
app:tabSelectedTextColor="#404040"
app:tabTextAppearance="@style/Mq_Body_s" />
其中的AppTheme.Main继承自Theme.MaterialComponents.Light.NoActionBar