问题描述
我使用的是compileSdk
和targetSdk
版本27,在上一发行版中,我为项目使用了新的字体资源功能,但是一天后,此代码行出现了3次崩溃
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);
,崩溃报告说这是由于android.content.res.Resources$NotFoundException
导致的,并且无法检索字体资源.使用Android版本5.1.1
的用户全部发生了3次崩溃.这是支持库中的错误,还是我做错了什么?
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?
推荐答案
遇到了同样的问题,注意到文档中的一个细节对以下问题有所帮助:
Had this same issue, noticed a detail in the docs that helped:
<?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>
我以前一直使用'android'命名空间,更改为'app'命名空间可以使我的字体正确加载到较旧的设备上.
I had been using the 'android' namespace before, changing to the 'app' namespace made my fonts load on older devices correctly.
这篇关于字体资源无法检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!