[英]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.