在 Android 10 / Android Q 上使用捆绑的 ttf 字体时崩溃

Crash when using bundled ttf font on Android 10 / Android Q

在我将我的 Android 应用程序的目标级别从 28 更新到 29 (Android 10) 后,该应用程序在 Pixel 3 上崩溃(Android 10)。

使用的版本

问题:这是一个已知问题吗?

详情

下面的堆栈跟踪。 出于安全原因,我已将应用程序包名称替换为 x.y.z。

2019-09-05 14:23:43.530 9060-9060/x.y.z E/AndroidRuntime: FATAL EXCEPTION: main
    Process: x.y.z, PID: 9060
    java.lang.RuntimeException: Unable to resume activity {x.y.z/x.y.main.devicefinder.BleFindActivity}: android.view.InflateException: Binary XML file line #73 in x.y.z:layout/fragment_add_devices: Binary XML file line #73 in x.y.z:layout/fragment_add_devices: Error inflating class TextView
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4205)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #73 in x.y.z:layout/fragment_add_devices: Binary XML file line #73 in x.y.z:layout/fragment_add_devices: Error inflating class TextView
     Caused by: android.view.InflateException: Binary XML file line #73 in x.y.z:layout/fragment_add_devices: Error inflating class TextView
     Caused by: java.lang.IndexOutOfBoundsException: index=0 out of bounds (limit=0, nb=4)
        at java.nio.Buffer.checkIndex(Buffer.java:564)
        at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:570)
        at android.graphics.fonts.FontFileUtil.analyzeStyle(FontFileUtil.java:94)
        at android.graphics.fonts.Font$Builder.build(Font.java:364)
        at android.graphics.Typeface$Builder.build(Typeface.java:596)
        at android.content.res.ResourcesImpl.loadFont(ResourcesImpl.java:980)
        at android.content.res.Resources.getFont(Resources.java:400)
        at android.content.res.TypedArray.getFont(TypedArray.java:1035)
        at android.widget.TextView.readTextAppearance(TextView.java:3991)
        at android.widget.TextView.<init>(TextView.java:1064)
        at android.widget.TextView.<init>(TextView.java:968)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:87)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:83)
        at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
        at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:242)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1061)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at x.y.main.devicefinder.BleFindFragment.onCreateView(BleFindFragment.java:62)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
2019-09-05 14:23:43.531 9060-9060/x.y.z E/AndroidRuntime:     at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3241)
        at androidx.fragment.app.FragmentController.dispatchResume(FragmentController.java:223)
        at androidx.fragment.app.FragmentActivity.onResumeFragments(FragmentActivity.java:538)
        at androidx.fragment.app.FragmentActivity.onPostResume(FragmentActivity.java:527)
        at androidx.appcompat.app.AppCompatActivity.onPostResume(AppCompatActivity.java:172)
        at android.app.Activity.performResume(Activity.java:7964)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4195)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

更新 1:我从库模块中复制了字体并包含在 app res 文件夹中,但仍然有这个问题。所以我更改了标题以使这个问题更通用。

更新 2:使用 .otf 字体时有效

我已将此事报告给 Google:https://issuetracker.google.com/issues/140546806 所以我想它现在变成了一个已知问题:-)

更新

根本原因:我的 ttf 文件是空的 (!)。我从来没有注意到,因为旧的机器人会默默地忽略这一点。

用其他支持的字体替换您现有的字体。

已经设置与现有字体相同的字体会引发异常并显示此消息

java.lang.IllegalArgumentException: Font {path=null, style=FontStyle { weight=400, slant=0}, ttcIndex=0, axes=, localeList=, buffer=java.nio.DirectByteBuffer[pos=0 lim=170012 cap=170012]} has already been added

也许这会对来这里查找此错误的人有所帮助