简体   繁体   English

充气时出错 class com.google.android.material.button.MaterialButton

[英]Error inflating class com.google.android.material.button.MaterialButton

What i'm trying to achieve is having a gridview with some materialButton inside.我想要实现的是有一个 gridview 里面有一些 materialButton。 I tried to create the gridview like:我试图创建 gridview 像:

<GridView
            android:id="@+id/login_gridview_code_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="3"
            android:horizontalSpacing="15dp"
            android:verticalSpacing="15dp">
        </GridView>

And the element to be inflated like:并且要膨胀的元素如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/button_code_digit"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            android:padding="0dp"
            app:iconGravity="textStart"
            app:iconPadding="0dp"
            app:iconSize="40dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.pswStorer.Button.Circle" />

</androidx.constraintlayout.widget.ConstraintLayout>

the adapter inflates the button:适配器为按钮充气:

view = inflter.inflate(R.layout.button_code, null); // inflate the layout

but I always receive this stacktrace:但我总是收到这个堆栈跟踪:

Error inflating class com.google.android.material.button.MaterialButton
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

I checked the appTheme but seems correct to me:我检查了 appTheme 但对我来说似乎是正确的:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="materialAlertDialogTheme">@style/AlertDialogMaterialTheme</item>
    </style>

Any ideas?有任何想法吗?

EDIT: I tried to check and my app has the correct theme编辑:我尝试检查并且我的应用具有正确的主题

android:theme="@style/AppTheme"

I also changed the theme to Theme.MaterialComponents.DayNight.NoActionBar.Bridge but nothing changed我还将主题更改为Theme.MaterialComponents.DayNight.NoActionBar.Bridge但没有任何改变

The issue is here.问题就在这里。

inflter = (LayoutInflater.from(applicationContext)); 

The Application context doesn't have your app theme.应用程序上下文没有您的应用程序主题。
You have to use a themed context like an Activity .您必须使用像Activity这样的主题上下文。

Check your AndroidManifest.xml that you are using right theme for your app also检查您的 AndroidManifest.xml 您是否也在为您的应用使用正确的主题

    <application
        android:theme="@style/AppTheme"
        ...>

change your theme改变你的主题

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">

to

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">

If you've got a separate theme for night don't forget to change to the Theme.MaterialComponents theme there too.如果你有一个单独的夜间主题,别忘了也改成 Theme.MaterialComponents 主题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二进制 XML 文件第 10 行:为 API 级别 19 增加类 com.google.android.material.button.MaterialButton 时出错 - Binary XML file line #10: Error inflating class com.google.android.material.button.MaterialButton for API Level 19 充气错误 class com.google.android.material.slider.Slider - Error inflating class com.google.android.material.slider.Slider 错误膨胀类 com.google.android.material.internal.NavigationMenuItemView - Error inflating class com.google.android.material.internal.NavigationMenuItemView 膨胀类com.google.android.material.tabs.TabLayout时出错-Android问题 - Error inflating class com.google.android.material.tabs.TabLayout - Android issue 升级应用程序获取错误膨胀类 com.google.android.material.navigation.NavigationView - upgrading app getting Error inflating class com.google.android.material.navigation.NavigationView 二进制 XML 文件第 24 行:错误膨胀类 com.google.android.material.navigation.NavigationView - Binary XML file line #24: Error inflating class com.google.android.material.navigation.NavigationView 二进制 XML 文件第 41 行:膨胀 class com.google.ZC31B32364CE19CA8FCD150A. - Binary XML file line #41: Error inflating class com.google.android.material.textfield.TextInputLayout 错误:在 androidX 中充气 class com.google.android.material.navigation.NavigationView - Error: inflating class com.google.android.material.navigation.NavigationView in androidX 原因:android.view.InflateException:二进制XML文件第16行:膨胀类com.google.android.material.navigation.NavigationView时出错 - Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.google.android.material.navigation.NavigationView Binary XML file line #21: Binary XML file line #21: Error inflating class com.google.android.material.textfield.TextInputLayout - Binary XML file line #21: Binary XML file line #21: Error inflating class com.google.android.material.textfield.TextInputLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM