9

I'm using the new Android Font support introduced in API 26 and backported in version 26 of the support library.

I've created a font_family.xml of two fonts like so:

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

I then set it on a TextView in my activity layout like so:

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

This works and renders the TextView in the correct font on a Nexus 5 running Marshmallow (using the support library). But it crashes when I try to run it on a Pixel Oreo device with the following stack:

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)

Looks like some error with inflating the font but can't deduce much more than that.

7
  • Which version of the support library are you using? – Samuel Robert Sep 20 '17 at 9:35
  • Support Library 26.0.2 – Valentin Sep 20 '17 at 9:38
  • 1
    Can you try with 26.1.0? may be this could be a bug in 26.0.2 – Samuel Robert Sep 20 '17 at 9:41
  • It worked for me with 26.1.0 and your current config looks okay – Samuel Robert Sep 20 '17 at 9:42
  • Same thing with 26.1.0 even after a complete clean and rebuild – Valentin Sep 20 '17 at 9:46
2

I had the same problem as you. So I changed from

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

to

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

inside the of my lobster_font_family.xml(v26) I made to use inside my demolayout.xml. And it's working on API 26 without a problem.

1
0

I found my problem. Apparently, when I copied the fonts from assets into res/fonts the regular_font did not copy over correctly and the file was corrupted. After replacing it with the proper file it worked.

It's still strange why this worked on pre-26 devices (using support lib) and crashed on Android Oreo (not running support lib)

1
  • Seeing the same error. Did you literally delete the old file and copy the new one? – Amilcar Andrade Nov 17 '17 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.