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

为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"
    
  • 额外检查

    1. 确保所有Activity都应用了Theme.CurrencyConverter主题,没有单独设置非Material主题
    2. 执行Build -> Clean Project,然后Rebuild Project清理缓存

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 02:20:38