Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font resource could not be retrieved

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?

like image 359
Amir_P Avatar asked Feb 16 '18 10:02

Amir_P


People also ask

What is a font resource?

A font resource defines a custom font that you can use in your app. Fonts can be individual font files or a collection of font files, known as a font family and defined in XML. Also see how to define fonts in XML or instead use downloadable fonts.

How do I Make my fonts load in XML?

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

What version of Google Play services do I need to use fonts?

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

What should the constant value of the font attribute be?

The constant value must be either normal or italic . Integer. The weight of the font. This attribute is used when the font is loaded into the font stack and overrides any weight information in the font's header tables.


2 Answers

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

like image 94
Vadim Kotov Avatar answered Oct 09 '22 10:10

Vadim Kotov


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.

like image 25
dwemthy Avatar answered Oct 09 '22 08:10

dwemthy