2017-09-20 43 views
7

我正在使用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奧利奧設備上運行它,它崩潰。

+0

您使用的是哪個版本的支持庫? –

+0

支持庫26.0.2 – Valentin

+1

你可以用'26.1.0'來試試嗎?可能這可能是26.0.2中的一個錯誤 –

回答

0

我發現我的問題。顯然,當我將資產中的字體複製到res/fonts中時,regular_font沒有正確複製並且文件已損壞。用適當的文件替換它後,它工作。

它仍然奇怪爲什麼這個工作預-26設備(使用支持LIB)和墜毀在Android奧利奧(不運行支持LIB)

+0

看到相同的錯誤。你是否真的刪除舊文件並複製新文件? –

1

我有同樣的問題,因爲你。所以,我從

<font-family xmlns:android="http://schemas.android.com/apk/res/android"> 
    <font 
     android:fontStyle="normal" 
     android:fontWeight="400" 
     android:font="@font/lobster_regular" /> 
    <font 
     android:fontStyle="italic" 
     android:fontWeight="400" 
     android:font="@font/lobster_italic" /> 
</font-family> 

改爲

<font-family xmlns:android="http://schemas.android.com/apk/res/android"> 
     <font 
      app:fontStyle="normal" 
      app:fontWeight="400" 
      app:font="@font/lobster_regular" /> 
     <font 
      app:fontStyle="italic" 
      app:fontWeight="400" 
      app:font="@font/lobster_italic" /> 
    </font-family> 

裏面我lobster_font_family.xml(V26),我做我的demolayout.xml內使用。它在API 26上工作沒有問題。

+0

將嘗試此解決方案 –

+0

這一個解決了我。 – YellowJ