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

使用com.google.android.material 1.6.1时,代码致App崩溃求助

崩溃原因排查与解决

常见崩溃原因及对应修复方案

1. 未使用MaterialComponents主题

Widget.MaterialComponents.TextInputLayout.OutlinedBox样式要求Activity或Application的主题必须继承自Theme.MaterialComponents(或其变体,如Theme.MaterialComponents.Light.DarkActionBar)。若使用传统AppCompat主题,会因缺少必要的主题属性引发崩溃。

修复:
修改AndroidManifest.xml中对应Activity的主题,或在res/values/styles.xml中定义继承MaterialComponents的主题:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- 自定义主题属性 -->
</style>

2. Hint管理冲突

使用OutlinedBox样式时,hint的显示逻辑由TextInputLayout统一管理,若在内部的TextInputEditText上设置hint,可能触发布局测量或样式渲染异常。

修复:
将android:hint和android:textColorHint移至TextInputLayout中:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/username"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:hint="Username"
    android:textColorHint="#757575">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

3. 依赖版本不兼容

若项目同时引入AppCompat库与Material库,版本不匹配会导致资源冲突。Material 1.6.1建议搭配AppCompat 1.4.0及以上版本使用。

修复:
检查Module级build.gradle中的依赖,确保版本兼容:

dependencies {
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.appcompat:appcompat:1.4.2'
    // 其他依赖
}

4. 布局结构不完整

用户提供的代码中LinearLayout仅包含起始标签,未添加闭合标签会导致XML解析错误,进而引发崩溃。

修复:
补充LinearLayout的闭合标签:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp">

    <!-- TextInputLayout代码 -->

</LinearLayout>

额外排查步骤

若以上方案无效,查看Logcat中的崩溃栈信息,重点关注InflateException或IllegalStateException的具体提示,比如属性缺失、样式引用错误等,进一步定位问题根源。

内容的提问来源于stack exchange,提问作者Fortis Leo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 23:35:41