错误膨胀 class com.google.android.material.textfield.TextInputLayout
Error inflating class com.google.android.material.textfield.TextInputLayout
这是我的应用主题:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
由于没有使用所需的主题,我收到了一个膨胀错误。在 Manifest
中,我没有覆盖 activity 的主题,因此出现此错误,因此使用了 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>
问题出在 material 版本上。
尝试更改您的版本 alpha06,它应该会修复它:
implementation 'com.google.android.material:material:1.1.0-alpha06'
尝试在父布局中应用此样式:
android:主题="@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">
我实现了这个:
implementation 'com.google.android.material:material:1.0.0'
然后我把样式改成:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
收件人:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
launchFragmentInContainer<LoginFragment>(themeResId =R.style.AppTheme)
在构造函数中添加主题!!
检查你的主题继承,在我的例子中,浅色主题是我从 material 组件继承的,而深色主题是从 material3
继承的
这是我的应用主题:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
由于没有使用所需的主题,我收到了一个膨胀错误。在 Manifest
中,我没有覆盖 activity 的主题,因此出现此错误,因此使用了 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>
问题出在 material 版本上。 尝试更改您的版本 alpha06,它应该会修复它:
implementation 'com.google.android.material:material:1.1.0-alpha06'
尝试在父布局中应用此样式:
android:主题="@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">
我实现了这个:
implementation 'com.google.android.material:material:1.0.0'
然后我把样式改成:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
收件人:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
launchFragmentInContainer<LoginFragment>(themeResId =R.style.AppTheme)
在构造函数中添加主题!!
检查你的主题继承,在我的例子中,浅色主题是我从 material 组件继承的,而深色主题是从 material3
继承的