Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash when using custom font for Toolbar

In my activity, I have a custom toolbar. I'm trying to change its title font.

Previously, I was able to do that by putting the font in the asset folder. With Android Studio 3, we can now use fontFamily and FontsContract. I tried this approach through the code below:

mToolbar.setTitleTextAppearance(context,R.style.AppTheme_ActionBarText);

and the style

<style name="AppTheme.ActionBarText" parent="@android:style/Widget.ActionBar.TabText">
            <item name="fontFamily">@font/ultra</item>
        </style>

When I run the app, after a few seconds (the time it takes to download the custom font), the app crashes with the log below:

java.lang.NullPointerException: Attempt to read from field 'int android.support.v4.provider.FontsContractCompat$TypefaceResult.mResult' on a null object reference at android.support.v4.provider.FontsContractCompat$2.onReply(FontsContractCompat.java:277) at android.support.v4.provider.FontsContractCompat$2.onReply(FontsContractCompat.java:274) at android.support.v4.provider.FontsContractCompat$3.onReply(FontsContractCompat.java:312) at android.support.v4.provider.FontsContractCompat$3.onReply(FontsContractCompat.java:300) at android.support.v4.provider.SelfDestructiveThread$2$1.run(SelfDestructiveThread.java:149) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Is this a bug or is there a correct way to load the font via xml? I know another way is to load the font programmatically and use the listener to set the typeface on the text.

**EDIT: on the second application run, no crash happens (since the font was already downloaded). For testing purposes, I'm changing the font after every crash to debug...

like image 957
usernotnull Avatar asked Oct 29 '17 21:10

usernotnull


3 Answers

Edit: It appears the issue has been fixed as of version 27.1.0 of the support library.

This appears to be a support library issue. Google devs have indicated that the fix should be available after version 27.0.2, which is currently unreleased.

https://issuetracker.google.com/issues/69085400

like image 175
TheIT Avatar answered Nov 17 '22 04:11

TheIT


Issue happen when user trying to start app with downloadable fonts when there isn't internet on device and this fonts wasn't downloaded previously. Then app will start, but crash will happen only after ~5-10 sech. I think when http timeout is happen.

So I haven't solution, therefore use xml-fonts instead of downloadable fonts, see https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

FYI: all downloadable fonts loaded to /data/data/com.google.android.gms/files/fonts folder onto your devices, If you have rooted device, then it easy to delete and reproduce this issue more that one time.

like image 25
adrbtk Avatar answered Nov 17 '22 03:11

adrbtk


According to docs you should use app namespace when using Support Library 26:

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

So in your code:

    <item name="app:fontFamily">@font/ultra</item>
like image 1
Micer Avatar answered Nov 17 '22 03:11

Micer