You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

三星Android 13设备应用XML加载崩溃(Typeface空指针)求助

三星Android 13设备TextInputLayout空指针崩溃解决方案

问题背景

目标API级别33的Android应用,仅在三星Android 13设备上出现崩溃,其余设备运行正常。Firebase捕获到的崩溃显示,XML布局加载时触发NullPointerException,核心是调用android.graphics.Typeface.getWeight()时Typeface对象为null。

崩溃核心信息

布局文件(edit_texts.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_input1_wrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/input_field_text_color"
        android:theme="@style/login_floating_text_style">

        <com.mobileaware.unified.ui.controls.common.MaEditText
            android:id="@+id/login_input1"
            style="@style/input_field_validation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:visibility="gone" />

    </com.google.android.material.textfield.TextInputLayout>

    <!-- 另外两个TextInputLayout结构类似 -->

</LinearLayout>

关键堆栈跟踪

Fatal Exception: android.view.InflateException
Binary XML file line #21 com.xyz.abc:layout/edit_texts: Attempt to invoke virtual method 'int android.graphics.Typeface.getWeight()' on a null object reference

Caused by java.lang.NullPointerException
Attempt to invoke virtual method 'int android.graphics.Typeface.getWeight()' on a null object reference

com.google.android.material.resources.TypefaceUtils.maybeCopyWithFontWeightAdjustment (TypefaceUtils.java:57)
com.google.android.material.internal.CollapsingTextHelper.setCollapsedTypefaceInternal (CollapsingTextHelper.java:480)
com.google.android.material.internal.CollapsingTextHelper.setTypefaces (CollapsingTextHelper.java:463)
com.google.android.material.textfield.TextInputLayout.setEditText (TextInputLayout.java:1426)
com.google.android.material.textfield.TextInputLayout.addView (TextInputLayout.java:836)

根本原因

  1. 三星Android 13系统的字体加载逻辑存在特殊性,导致自定义MaEditText初始化时Typeface为null
  2. 旧版本的Material Design组件未对Typeface为空的情况做防护,直接调用getWeight()触发空指针

可行解决方案

1. 升级Material组件版本(优先推荐)

将Material Design库升级到1.9.0及以上,官方已修复该空指针问题:

// 模块级build.gradle
dependencies {
    implementation 'com.google.android.material:material:1.9.0'
}

2. 确保自定义EditText的Typeface不为空

修改MaEditText的初始化逻辑,强制设置默认字体:

public class MaEditText extends EditText {
    public MaEditText(Context context) {
        super(context);
        initTypeface();
    }

    public MaEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTypeface();
    }

    public MaEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTypeface();
    }

    private void initTypeface() {
        if (getTypeface() == null) {
            setTypeface(Typeface.DEFAULT);
        }
    }
}

3. 显式设置TextInputLayout的字体样式

在login_floating_text_style中添加字体配置,避免浮动标签字体为空:

<style name="login_floating_text_style" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <!-- 保留原有属性,添加以下字体配置 -->
    <item name="android:fontFamily">sans-serif</item>
</style>

4. 禁用三星系统字体优化

在AndroidManifest.xml的application标签中添加属性,绕过三星的特殊字体处理:

<application
    ...
    android:allowFontScaling="false"
    android:fontFamily="sans-serif">
    ...
</application>

验证步骤

  • 使用三星Android 13设备安装修复后的APK
  • 重复触发崩溃场景(如进入登录页面)
  • 查看Firebase崩溃分析,确认该异常不再上报

内容的提问来源于stack exchange,提问作者SHIJO CS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 20:55:10