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

为Material 3的TextInputLayout设置样式致应用崩溃求助

问题原因及解决方案

崩溃根源

你使用了Material 3的TextInputLayout样式,但应用主题却是基于**Material Components(旧版)**的,两者版本不兼容,直接触发布局加载时的InflateException。

两种修复方案

方案1:切换到Material 3主题

将应用主题的父主题替换为Material 3版本,确保样式与主题版本匹配:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.EcomApp" parent="Theme.Material3.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/colorPrimary</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. -->
    </style>
</resources>

方案2:使用Material Components对应样式

如果不想升级到Material 3,将TextInputLayout的样式替换为Material Components旧版的对应样式:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/address_input_state"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="10dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
        app:layout_constraintTop_toBottomOf="@id/address_input_town_city"
        app:layout_constraintStart_toStartOf="@id/text_fill_address">

        <AutoCompleteTextView
            android:id="@+id/autocomplete"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="none"/>

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

额外注意事项

  • 确保Material Components依赖版本与主题/样式匹配:
    • 若用Material 3,依赖需为com.google.android.material:material:1.9.0+(或更高版本)
    • 若用旧版Material Components,依赖保持com.google.android.material:material:1.8.0-即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 20:50:29