2017-09-20 13 views
7

J'utilise le nouveau Android Font support introduit dans l'API 26 et rétroporté dans la version 26 de la bibliothèque de soutien.Android Oreo famille de polices NPE Crash

J'ai créé un font_family.xml de deux polices comme ceci:

<?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> 

Je puis mis sur un TextView dans ma mise en page d'activité comme ceci:

<TextView 
     style="@style/TextAppearance.Display1" 
     android:layout_width="wrap_content" 
     android:fontFamily="@font/font_family" 
     android:textStyle="bold" 
     android:layout_height="wrap_content" /> 

Cela fonctionne et rend le TextView dans la police correcte sur un Nexus 5 exécutant Marshmallow (en utilisant la bibliothèque de support). Mais il se bloque lorsque je tente de l'exécuter sur un périphérique Oreo pixels avec la pile suivante:

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) 

On dirait une erreur avec gonfler la police mais ne peut pas en déduire beaucoup plus que cela.

+0

Quelle version de la bibliothèque de support utilisez-vous? –

+0

Bibliothèque de support 26.0.2 – Valentin

+1

Pouvez-vous essayer avec '26.1.0'? peut-être cela pourrait être un bug dans 26.0.2 –

Répondre

0

J'ai trouvé mon problème. Apparemment, quand j'ai copié les polices des assets dans res/fonts le regular_font n'a pas copié correctement et le fichier a été corrompu. Après l'avoir remplacé par le bon fichier, cela a fonctionné.

Il est toujours étrange que cela a fonctionné sur les appareils pré-26 (en utilisant le soutien lib) et écrasé sur Android Oreo (pas de support en cours d'exécution lib)

+0

Voir la même erreur. Avez-vous littéralement supprimé l'ancien fichier et copié le nouveau? –

1

J'ai eu le même problème que vous. Alors j'ai changé de

<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> 

à l'intérieur du mon lobster_font_family.xml (v26) J'ai fait à utiliser à l'intérieur de mon demolayout.xml. Et cela fonctionne sur l'API 26 sans problème.

+0

Va essayer cette solution –

+0

Celui-ci a résolu pour moi. – YellowJ