简体   繁体   中英

Error inflating class com.google.android.material.button.MaterialButton

What i'm trying to achieve is having a gridview with some materialButton inside. I tried to create the gridview like:

<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:

<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

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 .

Check your AndroidManifest.xml that you are using right theme for your app also

    <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM