gpt4 book ai didi

android - 无法膨胀 MaterialCardView

转载 作者:行者123 更新时间:2023-12-05 05:02:16 25 4
gpt4 key购买 nike

我收到以下错误:

    E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mainapp, PID: 21456
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:658)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:827)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:900)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:861)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.mainapp.ListCategories$MyAdapter.getView(ListCategories.java:99)
at android.widget.AbsListView.obtainView(AbsListView.java:2413)
at android.widget.ListView.makeAndAddView(ListView.java:2074)
at android.widget.ListView.fillDown(ListView.java:801)
at android.widget.ListView.fillFromTop(ListView.java:862)
at android.widget.ListView.layoutChildren(ListView.java:1841)
at android.widget.AbsListView.onLayout(AbsListView.java:2212)
at android.view.View.layout(View.java:21112)
at android.view.ViewGroup.layout(ViewGroup.java:6400)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1828)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:21112)
at android.view.ViewGroup.layout(ViewGroup.java:6400)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:21112)
at android.view.ViewGroup.layout(ViewGroup.java:6400)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1828)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:21112)
at android.view.ViewGroup.layout(ViewGroup.java:6400)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:21112)
at android.view.ViewGroup.layout(ViewGroup.java:6400)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1828)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:21112)
at android.view.ViewGroup.layout(ViewGroup.java:6400)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:883)
at android.view.View.layout(View.java:21112)
at android.view.ViewGroup.layout(ViewGroup.java:6400)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3508)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1931)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8368)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1088)
at android.view.Choreographer.doCallbacks(Choreographer.java:900)
at android.view.Choreographer.doFrame(Choreographer.java:824)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1074)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7210)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:961)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:248)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:222)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:150)
at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:81)
at com.google.android.material.card.MaterialCardView.<init>(MaterialCardView.java:124)
at com.google.android.material.card.MaterialCardView.<init>(MaterialCardView.java:114)
... 61 more

我的xml布局第11行

<com.google.android.material.card.MaterialCardView

使用的 gradle 版本:

implementation 'com.google.android.material:material:1.1.0'

list 应用主题

android:theme="@style/AppTheme.MaterialComponents"

应用主题风格

<style name="NoActionBarHome" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

</style>
<!-- Base application theme. -->
<style name="AppTheme.MaterialComponents" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>

我用来调用布局文件的代码

布局文件名:R.layout.row

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row, parent, false);
images = row.findViewById(R.id.image);
myTitle = row.findViewById(R.id.textView1);
myDescription = row.findViewById(R.id.textView2);
// now set our resources on views
Bitmap bmImg = BitmapFactory.decodeFile(rImgs[position]);
images.setImageBitmap(bmImg);
myTitle.setText(rTitle[position]);
myDescription.setText(rDescription[position]);

return row;
}

如果您需要更多信息,请告诉我,谢谢

最佳答案

问题是:

 Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

适配器中使用的上下文存在问题。您必须传入 Activity,而不是 ApplicationContext
ApplicationContext 没有应用主题。

在你的代码更改中

 layoutInflater = (LayoutInflater) 
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  layoutInflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

其中 contextActivity

关于android - 无法膨胀 MaterialCardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62269440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com