字体资源无法检索 [英] Font resource could not be retrieved

查看:138
本文介绍了字体资源无法检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是compileSdktargetSdk版本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布局中声明字体系列时 库,请使用应用程序名称空间来确保字体加载.

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>

我以前一直使用'android'命名空间,更改为'app'命名空间可以使我的字体正确加载到较旧的设备上.

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

这篇关于字体资源无法检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆