com.google.android.material.button.MaterialButton类膨胀错误

7
我想实现的目标是创建一个带有一些材质按钮的网格视图。 我试着像这样创建网格视图:
<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>

需要被填充的元素如下:

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

适配器填充按钮。
inflter = (LayoutInflater.from(applicationContext)); 
view = inflter.inflate(R.layout.button_code, null); // inflate the layout

但是我总是收到这个堆栈跟踪:

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

我检查了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>

有什么想法吗?

编辑:我尝试进行检查,我的应用程序具有正确的主题。

android:theme="@style/AppTheme"

我也将主题改为Theme.MaterialComponents.DayNight.NoActionBar.Bridge,但是没有任何变化。


你如何获取 inflter - Gabriele Mariotti
@GabrieleMariotti inflter = (LayoutInflater.from(applicationContext));@GabrieleMariotti inflter = (LayoutInflater.from(applicationContext)); - Anon
您可以在下面找到答案。问题出在应用程序上下文中,它没有应用主题。 - Gabriele Mariotti
4个回答

6

更改您的主题

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

to

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

尝试过但结果相同。 - Anon
这对我有用。我切换到MaterialButton,但我的主题仍然是默认的。 - William

2
这里出现了问题。
inflter = (LayoutInflater.from(applicationContext)); 

应用程序上下文没有您的应用主题。
您必须使用一个带有主题的上下文,比如Activity


1
谢谢,使用上下文是解决方法。 - Anon

0

请检查您的 AndroidManifest.xml 文件,确保您的应用程序使用了正确的主题

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

0

如果您有一个独立的夜间主题,请不要忘记在那里也更改为Theme.MaterialComponents主题。


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