我正在使用API 26中引入的新的Android Font support,並在支持庫的第26版中向後移植。Android Oreo Font Family NPE崩潰
我創建了兩個font_family.xml
一個字體,像這樣:
<?xml version="1.0" encoding="utf-8"?>
<font-family
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
android:font="@font/regular_font"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/regular_font"
app:fontStyle="normal"
app:fontWeight="400"/>
<font
android:font="@font/bold_font"
android:fontStyle="normal"
android:fontWeight="700"
app:font="@font/bold_font"
app:fontStyle="normal"
app:fontWeight="700"/>
</font-family>
然後我把它放在一個TextView在我的活動佈局像這樣:
<TextView
style="@style/TextAppearance.Display1"
android:layout_width="wrap_content"
android:fontFamily="@font/font_family"
android:textStyle="bold"
android:layout_height="wrap_content" />
這工作和呈現的TextView在運行棉花糖的Nexus 5上使用正確的字體(使用支持庫)。
Caused by: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class TextView
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at android.support.v4.graphics.TypefaceCompatApi26Impl.abortCreation(TypefaceCompatApi26Impl.java:202)
at android.support.v4.graphics.TypefaceCompatApi26Impl.createFromFontFamilyFilesResourceEntry(TypefaceCompatApi26Impl.java:220)
at android.support.v4.graphics.TypefaceCompat.createFromResourcesFamilyXml(TypefaceCompat.java:116)
at android.support.v4.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:249)
at android.support.v4.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:213)
at android.support.v4.content.res.ResourcesCompat.getFont(ResourcesCompat.java:206)
at android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119)
at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:208)
看起來有些錯誤與膨脹的字體,但不能推斷遠不止這些:但是,當我嘗試以下堆棧中的Pixel奧利奧設備上運行它,它崩潰。
您使用的是哪個版本的支持庫? –
支持庫26.0.2 – Valentin
你可以用'26.1.0'來試試嗎?可能這可能是26.0.2中的一個錯誤 –