Font resource could not be retrieved
Asked Answered
F

6

30

I'm using compileSdk and targetSdk version 27 and in last release I used new font resource feature for my project but after a day I got 3 crashes for this line of code

Typeface typeface = ResourcesCompat.getFont(this, R.font.my_font);

and the crash report says it's because of android.content.res.Resources$NotFoundException and Font resource could not be retrieved. All 3 crashes happened for users with android version 5.1.1. Is this a bug in support library or I'm doing something wrong?

Fling answered 16/2, 2018 at 10:43 Comment(6)
getFont was added in api version 26 developer.android.com/reference/android/support/v4/content/res/…Allyson
but I tested and it's working on lower api's @lib4Fling
Would you mind removing the underscore from the font file? Sometimes it is reported similar issue #43142735Allyson
that's just an example. my font name is something different and doesn't have underscore or other special character @lib4Fling
@Fling Did you find a solution to fix this?Wichman
Unfortunately I can't remember what I did to avoid the issue :( @WichmanFling
G
10

I got the same crash while using Downloadable fonts on API level 16 with Google Play services 9.2.56 (emulator).

If you're using this, then a device must have Google Play services version 11 or higher to use the Google Fonts provider (see this note in the docs).

Gerik answered 7/2, 2019 at 14:57 Comment(5)
Thanks for your answer but I'm not using downloadable fontsFling
@Shrikant I just used a different emulator. On a real device you shouldn't encounter this error, because this version of Google Play services is very old.Gerik
@VadimKotov But I'm getting this issue on real deviceNonparticipating
@Nonparticipating I've got some crash reports from real devices too, I think this discussion can be helpful. Will try to refactor my code according to the link.Gerik
How to test the version of Google Play services?Landon
S
9

Had this same issue, noticed a detail in the docs that helped:

When you declare font families in XML layout through the support library, use the app namespace to ensure your fonts load.

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
  <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
  <font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />
</font-family>

I had been using the 'android' namespace before, changing to the 'app' namespace made my fonts load on older devices correctly.

Smallman answered 20/6, 2018 at 21:44 Comment(2)
Didn't work out for the downloadable fonts that I am using. :(Astrology
seems to do the job, thanks. tested ok. did not try downloadable fonts though.Sarmatia
C
3

I had the same problem with a TTF file in R.font when using ResourcesCompat.getFont().

It turns out that Android didn't like this TTF file for some reason. There was no info in logcat, but debugging showed that TypefaceCompat.createFromResourcesFontFile() failed.

I replaced the TTF file with another, similar font and had no problems since.

Coypu answered 10/7, 2019 at 10:6 Comment(0)
S
0

In my case, even after upgrading Gradle version to most recent one didn't work. What worked for me was a really trivial solution, updating the dependencies version to latest versions. I don't know it might work or not in your case, but maybe you should try doing it once.

Shimmy answered 27/6, 2020 at 5:5 Comment(0)
B
0

while using Downloadable fonts on API level 16

The fonts need to be downloaded so its better to check the Internet connection and download them, even when they are provided from the fonts folder.

Buttocks answered 13/5, 2021 at 10:38 Comment(0)
H
0

I was having the same issue, so I followed another approach and it work fine for me.

  1. Add a new directory in the app/src/main named as assets and make another subdirectory inside it as fonts

  2. Add a font file (eg. poppins.ttf) inside the fonts subdirectory

  3. If you are a Java developer then write this code in your Activity/Fragment file-

    Typeface font = Typeface.createFromAsset(getAssets(),"fonts/poppins.ttf"); 
    yourTextView.setTypeface(font);
    

    and if you are a Kotlin developer then you can use this,

    val customTypeFace = Typeface.createFromAsset(context.assets, 
                                                  "fonts/poppins-Light.ttf")
    
Helenehelenka answered 8/5, 2022 at 1:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.