解析 com.google.android.material.textfield.TextInputLayout 类时出错

21

这是我的应用程序主题:<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

我遇到了一个由于未使用所需的主题而导致的膨胀错误。在Manifest中,我没有覆盖导致此错误的活动的主题,因此使用了在styles.xml中定义的AppTheme。

错误:

 android.view.InflateException: Binary XML file line #122: Binary XML file line #122: Error inflating class com.google.android.material.textfield.TextInputLayout
    Caused by: android.view.InflateException: Binary XML file line #122: Error inflating class com.google.android.material.textfield.TextInputLayout
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
      ...
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240)
        at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215)
        at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143)
        at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:116)

这是我的xml文件:

  <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/inputFirstName"
                        style="@style/EditText.OutlinedBox"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginEnd="16dp"
                        android:visibility="gone"
                        app:boxStrokeColor="@color/colorBrand"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent">

以及EditText的样式:

 <style name="EditText.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="android:minHeight">56dp</item>
        <item name="android:textSize">18sp</item>
        <item name="android:fontFamily">@font/nunito_regular</item>
        <item name="android:textColorHint">@color/colorTextSecondary</item>
        <item name="hintTextColor">@color/colorBrand</item>
        <item name="hintEnabled">true</item>
    </style>

这个应用的目标SDK版本是29,我使用的是material_design_components_version = '1.1.0-alpha08'

编辑:

来自app/gradle

    implementation(
        "androidx.appcompat:appcompat:$appcompat_version",
        "androidx.constraintlayout:constraintlayout:$constraintlayout_version",
        "com.google.android.material:material:$material_design_components_version"
)

清单:

<application
    android:name=".application.BaseApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

主题:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowLightStatusBar">true</item>
    <item name="android:fontFamily">@font/nunito_regular</item>
    <item name="android:lineSpacingExtra">0dp</item>
    <item name="android:includeFontPadding">false</item>
    <item name="navigationIcon">@drawable/ic_back_dark</item>
    <item name="android:windowBackground">@color/colorWhite</item>
    <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>

将 android:theme="Theme.MaterialComponents" 添加到布局中。 - Rajnish suryavanshi
无法工作,崩溃并显示相同的消息。 - ghita
1
请展示一下您在Gradle中的设计参考是什么样子。看起来您正在混合使用AppCompat和AndroidX。请还要添加一下您清单文件的外观 - 特别是应用主题。 - ror
@ror 我已经添加了详细信息。 - ghita
你解决了那个问题吗?我也遇到了这个问题,但是还没有找到任何解决方法。@ghita - aligur
显示剩余4条评论
5个回答

53

在父布局中尝试应用以下样式:

android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical"
        android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar">
        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        <com.google.android.material.textfield.TextInputEditText
       
        </com.google.android.material.textfield.TextInputLayout>
   </LinearLayout>

这是一个示例,为了完整使用,请在您的应用程序样式中放置父级标签。例如:

<style name="MyApp" parent="Theme.MaterialComponents">

在布局中设置主题可以成为一种有用的调试工具。然而,典型的应用程序会在 AndroidManifest.xml 中设置应用程序范围的主题。该主题通常在 values/themes.xml 中定义,并且应该有一个父级为 Theme.MaterialComponents.DayNight.DarkActionBar(或类似)。 - Matt

7
我实现了这个:
implementation 'com.google.android.material:material:1.0.0'

然后我将样式从以下方式更改:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

To:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

4
 launchFragmentInContainer<LoginFragment>(themeResId =R.style.AppTheme)

在构造函数中添加主题!


这是我需要做的唯一事情。非常顺利地完成了,谢谢! - renatoarg
@renatoarg 您好,很高兴听到它能为某人带来帮助。 - Nurseyit Tursunkulov
感谢分享解决方案。对我来说真是救命稻草。 - Subratsss

1
问题出在材料版本上。 尝试更改您的版本为alpha06,应该可以解决问题:
implementation 'com.google.android.material:material:1.1.0-alpha06'

6
不,问题在于TextInput位于对话框内部,我为对话框使用的上下文是Activity上下文而不是Fragment上下文,这导致打开对话框。 - ghita
这对我来说似乎很好用。谢谢。 - Md Nakibul Hassan

1

请检查您的主题继承,在我的情况下,浅色主题使用了 material 组件进行继承,而深色主题则使用了 material3 进行继承。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接