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