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

XML布局第125行报错:Error inflating class TextInputLayout求助

解决 Error inflating class com.google.android.material.textfield.TextInputLayout 问题

已经尝试过MaterialComponents主题修复仍未解决的话,可从以下几个方向排查:

  • 检查Material组件依赖兼容性
    确认build.gradle(module级)中Material组件的依赖版本与项目的compileSdkVersion、targetSdkVersion匹配,比如:

    dependencies {
        implementation 'com.google.android.material:material:1.11.0' // 对应compileSdk 34
    }
    

    避免出现依赖版本过低或与SDK版本不匹配的情况。

  • 修正布局中的属性错误
    看你提供的布局代码,存在两个可能触发Inflate错误的属性:

    1. android:textSize="@dimen/title_size":TextInputLayout不支持直接通过android:textSize设置提示文字大小,需改用app:hintTextAppearance指定样式,比如:
      app:hintTextAppearance="@style/HintTextStyle"
      
      然后在styles.xml中定义该样式:
      <style name="HintTextStyle" parent="TextAppearance.MaterialComponents.Hint">
          <item name="android:textSize">@dimen/title_size</item>
      </style>
      
    2. android:maxLength="@dimen/max_group_length":maxLength要求引用整数资源,而非尺寸资源(dimen)。需在res/values/integers.xml中定义:
      <integer name="max_group_length">20</integer>
      
      再在布局中引用@integer/max_group_length。
  • 验证资源引用的有效性
    检查布局中引用的所有资源(@color/low_importance_green、@dimen/horizontal_gap、@string/group_ticket_title等)是否存在,拼写是否正确,颜色资源是否为有效的色值格式。

  • 确认主题的全局覆盖情况
    即使Activity设置了MaterialComponents主题,也要检查是否在布局的父容器或该TextInputLayout本身设置了冲突的主题属性,比如android:theme="@style/Theme.AppCompat",这种局部主题会覆盖全局主题,导致Material组件无法正常初始化。

  • 排查混淆规则(仅发布版)
    如果是发布版出现该问题,检查proguard-rules.pro是否正确保留了Material组件类:

    -keep class com.google.android.material.** { *; }
    -keep class androidx.appcompat.widget.** { *; }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 04:35:14