样式的编写,易出的错误
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.TabLayout
... ...
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.res.ColorStateList.getDefaultColor()' on a null object reference
根据错误提示,可知,是布局中的TabLayout书写出错了,在根据
后边的提示--getDefaultColor(),可知是没有设置默认颜色,
因为布局当中没有设置字体颜色,并且改变了默认样式,
在布局中给TabLayout设置默认颜色,或者让样式继承默认样式,
即解决错误。
错误写法示例
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="46dp"
app:tabTextAppearance="@style/CustomTabLayoutTextAppearance"
/>
<style name="CustomTabLayoutTextAppearance"
<item name="android:textSize">20sp</item>
</style>
正确写法
第一种写法
样式的编写
<style name="CustomTabLayoutTextAppearance" >
//设置字体的大小
<item name="android:textSize">20sp</item>
</style>
布局的编写
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="46dp"
app:tabIndicatorColor="@color/tab_selected_line_def"
app:tabSelectedTextColor="@color/tab_text_selected_def"
//设置后,即可改变样式,并可不继承默认样式,否则报错
app:tabTextColor="@color/tab_text_normal_def"
app:tabTextAppearance="@style/CustomTabLayoutTextAppearance"
/>
第二种写法
样式的编写
<style name="CustomTabLayoutTextAppearance">
//设置字体的大小
<item name="android:textSize">20sp</item>
<item name="android:textColor">#0d77e9</item>
</style>
布局的编写
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="46dp"
app:tabIndicatorColor="@color/tab_selected_line_def"
app:tabSelectedTextColor="@color/tab_text_selected_def"
app:tabTextAppearance="@style/CustomTabLayoutTextAppearance"
/>
第三种写法
样式的编写
<style name="CustomTabLayoutTextAppearance"
继承默认的样式 即可不设置颜色,有默认的颜色
parent=
"TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
<item name="android:textSize">20sp</item>
</style>
布局的编写
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="46dp"
app:tabIndicatorColor="@color/tab_selected_line_def"
app:tabSelectedTextColor="@color/tab_text_selected_def"
app:tabTextAppearance="@style/CustomTabLayoutTextAppearance"
/>