Android Oreo Font Family NPE Crash

I am using the new Android Font Support introduced in API 26 and is included in version 26 of the support library.

I created font_family.xml two such fonts:

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

Then I set it to TextView in my activity layout as follows:

 <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 displays the TextView in the correct font on the Nexus 5 working with 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) 

It looks like some kind of bug with bloating the font, but cannot output much more.

+7
android android-layout fonts textview android-8.0-oreo
source share
2 answers

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

It's still weird why this worked on pre-26 devices (using lib support) and crashed on Android Oreo (lib support doesn't work)

0
source share

I had the same problem as you. Therefore, I have 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 my lobster_font_family.xml (v26), which I used to use in my demolayout.xml. And he works on API 26 without any problems.

+1
source share

All Articles