package comexamplematerialtestimport androidxappcompatappAppCompatActivityimport androidosBundleimport androidviewMenuimport androidviewMenuItemimport androidwidgetPopupMenuimport androidwidgetToastim
根据报错信息,可以看出是在FruitAdapter的onCreateViewHolder方法中出现了错误。具体错误信息是java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant),意思是MaterialCardView组件需要使用Theme.MaterialComponents或其子主题。
解决方法是确保你的App主题是Theme.MaterialComponents或其子主题。可以在styles.xml中的AppTheme中添加parent属性,继承自Theme.MaterialComponents,如下所示:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
</style>
然后在AndroidManifest.xml中将AppTheme应用到MainActivity上,如下所示:
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme" />
保存并重新运行应用程序,错误应该就会被解决
原文地址: https://www.cveoy.top/t/topic/iSa7 著作权归作者所有。请勿转载和采集!