为TextInputLayout添加Material3样式后应用崩溃,求解决方案
TextInputLayout添加样式后崩溃的解决方法
添加TextInputLayout样式后应用崩溃,报错信息:
This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant)
已确认主题继承自MaterialComponents,使用的依赖版本为:
implementation 'com.google.android.material:material:1.9.0-alpha01'
布局代码
<com.google.android.material.textfield.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:layout_marginEnd="16dp" app:helperText="required" app:helperTextTextColor="@color/Red" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"> <AutoCompleteTextView android:id="@+id/from_auto_complete_tV" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="none" android:text="Dollar" /> </com.google.android.material.textfield.TextInputLayout>
主题代码
<resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Theme.CurrencyConverter" parent="Theme.MaterialComponents.Light.NoActionBar"> <!-- Primary brand color. --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <!-- Secondary brand color. --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorOnSecondary">@color/black</item> <!-- Status bar color. --> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> </style> </resources>
解决方法
问题核心是Material2主题与Material3组件样式不兼容:你使用了Material3的Widget.Material3.*样式,但主题是Material2的Theme.MaterialComponents.*,版本不匹配导致TextAppearance属性缺失。
方案1:切换为Material3主题
修改主题的parent为Material3版本:<style name="Theme.CurrencyConverter" parent="Theme.Material3.Light.NoActionBar"> <!-- 保留原有的颜色配置 --> </style>方案2:使用Material2的组件样式
将TextInputLayout的style替换为Material2版本:style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"额外检查
- 确保所有Activity都应用了
Theme.CurrencyConverter主题,没有单独设置非Material主题 - 执行
Build -> Clean Project,然后Rebuild Project清理缓存
- 确保所有Activity都应用了
内容的提问来源于stack exchange,提问作者ahmed salama
相关产品推荐
相关产品推荐

