Android Studio中存在应用程序命名空间格式错误的XML字体系列文件



在我的Android Studio项目中,我有一个名为"cutive.xml"的字体文件

文件路径为:resources->字体->cutive.xml

文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res/android" //Lint problem here
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Cutive"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">

正如您所看到的,应用程序命名空间的格式不正确:xmlns:app="http://schemas.android.com/apk/res/android">

在MainActivity中,我创建了一个引用字体的静态Typeface对象:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
typeface0024 = getResources().getFont(R.font.cutive); //app will crash if app namespace is corrected
else typeface0024 = Typeface.create(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);

即使Lint声称存在错误,该应用程序也能正确加载并找到字体。当我将应用程序名称空间切换到正确的形式时:

xmlns:app="http://schemas.android.com/apk/res-auto">
应用程序在线路上崩溃:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
typeface0024 = getResources().getFont(R.font.cutive);

错误日志给出了以下原因:运行时异常

引起原因:android.content.res.Resources$NotFoundException:字体资源ID#0x7f090000

如何修复林特给出的错误?或者,我应该只是抑制错误,即使应用程序名称空间的格式不正确,字体和应用程序也会正确加载。

任何建议

我能够通过使用"android"命名空间而不是"app"命名空间来解决这个问题。

根据Android文档,请确保您拥有以下支持库:

编译"com.android.support:support-compat:28.0.0">

现在它工作了,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"

android:fontProviderAuthority="com.google.android.gms.fonts"
android:fontProviderPackage="com.google.android.gms"
android:fontProviderQuery="Cutive"
android:fontProviderCerts="@array/com_google_android_gms_fonts_certs">

</font-family>

最新更新