Singa
Singa

Reputation: 371

Cannot inflating MaterialCardView

I get the following error :

    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

My xml layout line 11

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

Used gradle version :

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

Manifest App theme

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

Style App theme

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

Code that i use to call my layout file

layout file name : 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;
        }

If you need any more information, please let me know, thank you

Upvotes: 3

Views: 2610

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363815

The issue is:

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

There is an issue with the context used in the Adapter. You have to pass in the Activity, not an ApplicationContext.
The ApplicationContext hasn't the app theme.

In your code change

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

to

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

where context is the Activity.

Upvotes: 2

Related Questions