Font Family cause NPE on android 26
Asked Answered
P

4

9

My App crashes on android 26, my understanding is that it's caused by font-family and support library, but I can't figured out why. On devices below Android 8.0 it work fine.

Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class Button
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at android.support.v4.graphics.TypefaceCompatApi26Impl.addFontFromAssetManager(TypefaceCompatApi26Impl.java:150)
at android.support.v4.graphics.TypefaceCompatApi26Impl.createFromFontFamilyFilesResourceEntry(TypefaceCompatApi26Impl.java:218)
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)
at android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:152)
at android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:77)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:67)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v4.graphics.TypefaceCompatApi26Impl.addFontFromAssetManager(TypefaceCompatApi26Impl.java:145)
... 42 more
Caused by: java.lang.NullPointerException
at android.graphics.FontFamily.nAddFontFromAssetManager(Native Method)
at android.graphics.FontFamily.addFontFromAssetManager(FontFamily.java:149)
... 44 more
Parasitism answered 29/9, 2017 at 9:4 Comment(0)
P
6

By using only xmlns:app it's work for android v26

<font
    app:font="@font/cuprum_regular"
    app:fontStyle="normal"
    app:fontWeight="400"/>
Parasitism answered 29/9, 2017 at 9:35 Comment(0)
G
3

From Fonts in XML docs

The Support Library 26.0 provides support to the Fonts in XML feature on devices running Android API version 14 and higher.

When you declare font families in XML layout through the support library, use the app namespace.

Since your app is supporting API 14 so you have to use app namespace instead of android

Generative answered 29/9, 2017 at 9:48 Comment(0)
Z
1

I had the same issue that the OP was referring to, with practically the same crash stacktrace.

I noticed that this was fixed for me when I switched to using the 27.0.2 version of the appcompat support library when building using Gradle.

For custom AOSP app developers who might be depending on the framework provided support library in order to build your font API-enabled app, you can refer to this commit for the fix: https://android.googlesource.com/platform/frameworks/support/+/d96eeb46571f2d01c63d21d3aee4ba9bf4b10c53

If you're developing using Android Studio/Gradle, just updating support libraries to 27.0.2 or newer should do it.

Zeeba answered 26/4, 2018 at 21:15 Comment(0)
O
0

I'm using support library version 27.0.1 and I had the exact same crash on Android Oreo when I was using downloadable fonts https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html
Instead of using downloadable fonts I just downloaded the fonts from Google fonts website and placed them in res\fonts directory. The font family xml file remained the same. I tested it and it woks on all Android versions. It's either a bug or I didn't setup the downloadable fonts correctly even though I used the built in wizard to add a new font.

Ottar answered 18/11, 2017 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.