12

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

usernotnull
  • 3,480
  • 4
  • 25
  • 40

4 Answers4

5

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.

adrbtk
  • 4,470
  • 1
  • 12
  • 11
  • This is what I already did long time ago, and it seems there still is no way to do it programmatically, so I'll mark it as solved in case others want to refer to it. Thanks. – usernotnull Dec 16 '17 at 08:40
4

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

TheIT
  • 11,919
  • 4
  • 64
  • 56
1

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>
Micer
  • 8,731
  • 3
  • 79
  • 73
  • In the styles.xml you can't really use app namespace, so to edit your above, just strip "app:". But still, the issue still was not resolved :/ It's something else, I edited my question – usernotnull Oct 29 '17 at 21:47
0

This issue might happen when you run the application first time without any internet connection. Check your internet connection and try again.

6155031
  • 4,171
  • 6
  • 27
  • 56