Android Oreo 字体系列 NPE 崩溃

Android Oreo Font Family NPE Crash

我正在使用 Android Font support 在 API 26 中引入并在支持库的版本 26 中向后移植。

我创建了一个 font_family.xml 两种字体,如下所示:

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

    <font
        android:font="@font/regular_font"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/regular_font"
        app:fontStyle="normal"
        app:fontWeight="400"/>

    <font
        android:font="@font/bold_font"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/bold_font"
        app:fontStyle="normal"
        app:fontWeight="700"/>

</font-family>

然后我将它设置在我的 activity 布局中的 TextView 上,如下所示:

<TextView
        style="@style/TextAppearance.Display1"
        android:layout_width="wrap_content"
        android:fontFamily="@font/font_family"
        android:textStyle="bold"
        android:layout_height="wrap_content" />

这有效并在 Nexus 5 运行ning Marshmallow 上以正确的字体呈现 TextView(使用支持库)。但是当我尝试在具有以下堆栈的 Pixel Oreo 设备上 运行 它时它崩溃了:

Caused by: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class TextView
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
    at android.support.v4.graphics.TypefaceCompatApi26Impl.abortCreation(TypefaceCompatApi26Impl.java:202)
    at android.support.v4.graphics.TypefaceCompatApi26Impl.createFromFontFamilyFilesResourceEntry(TypefaceCompatApi26Impl.java:220)
    at android.support.v4.graphics.TypefaceCompat.createFromResourcesFamilyXml(TypefaceCompat.java:116)
    at android.support.v4.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:249)
    at android.support.v4.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:213)
    at android.support.v4.content.res.ResourcesCompat.getFont(ResourcesCompat.java:206)
    at android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119)
    at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:208)

看起来像是膨胀字体的一些错误,但不能推断出更多。

我发现了我的问题。显然,当我将字体从资产复制到 res/fonts 时,regular_font 没有正确复制并且文件已损坏。用正确的文件替换它后,它工作了。

为什么这在 pre-26 设备(使用支持库)上工作并在 Android Oreo(不是 运行 支持库)上崩溃仍然很奇怪

我遇到了和你一样的问题。所以我从

<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

<font-family xmlns:android="http://schemas.android.com/apk/res/android">
        <font
            app:fontStyle="normal"
            app:fontWeight="400"
            app:font="@font/lobster_regular" />
        <font
            app:fontStyle="italic"
            app:fontWeight="400"
            app:font="@font/lobster_italic" />
    </font-family>

在我的 lobster_font_family.xml(v26) 中,我在 demolayout.xml 中使用。它正在 API 26 上正常工作。