Android Oreo字体系列NPE崩溃

Val*_*tin 8 fonts android textview android-layout android-8.0-oreo

我正在使用API 26中引入的新Android 字体支持,并在支持库的第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>
Run Code Online (Sandbox Code Playgroud)

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

<TextView
        style="@style/TextAppearance.Display1"
        android:layout_width="wrap_content"
        android:fontFamily="@font/font_family"
        android:textStyle="bold"
        android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

这可以在运行Marshmallow的Nexus 5上使用正确的字体呈现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)
Run Code Online (Sandbox Code Playgroud)

看起来像膨胀字体有些错误,但不能推断出更多.

Val*_*tin 0

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

仍然很奇怪为什么这在 26 之前的设备上有效(使用支持库)而在 Android Oreo 上崩溃(不运行支持库)