MaterialCardView crashes with material:1.1.0
S

11

43

I am using MaterialCardView as my cardview layout. Now, Android-studio started showing me that instead of current implementation "com.google.android.material:material:1.0.0", I should update to 1.1.0

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/edit_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:layout_margin="@dimen/activity_vertical_margin">

  <com.google.android.material.card.MaterialCardView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:cardCornerRadius="8dp"
      app:cardElevation="8dp">

      <...more layout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>

with 1.1.0, I am getting crash Error inflating class com.google.android.material.card.MaterialCardView

So, I have 3 cases:

  1. stick to 1.0.0 and use MaterialCardView (working, but not preferred)
  2. Update to 1.1.0 (or 1.2.0-alpha) and use androidx.CardView (working, but not preferred)
  3. Update to 1.1.0 and change the MaterialCardView (preferred, but not working)

Since I want to go with option #3, and I don't have any solution, can anybody give a clue?

Update My style (I have style & style-night also. Here I am posting the style only):

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>

  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

  <style name="ThemeOverlay.AppCompat.navTheme">
    <!-- Color of text and icon when SELECTED -->
    <item name="colorPrimary">#ffffff</item>
    <!-- Background color when SELECTED -->
    <item name="colorControlHighlight">@color/colorPrimary</item>

  </style>

</resources>

And here is the error(from Logcat)

2020-02-12 13:34:18.171 12562-12562/com.example.phocast E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.phocast, PID: 12562
    android.view.InflateException: Binary XML file line #10 in com.example.phocast:layout/fragment_place: Binary XML file line #10 in com.example.phocast:layout/fragment_place: Error inflating class com.google.android.material.card.MaterialCardView
    Caused by: android.view.InflateException: Binary XML file line #10 in com.example.phocast:layout/fragment_place: Error inflating class com.google.android.material.card.MaterialCardView
    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:854)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at com.example.phocast.MainActivity.onOptionsItemSelected(MainActivity.java:555)
        at android.app.Activity.onMenuItemSelected(Activity.java:4137)
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:383)
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:219)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:64)
        at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:207)
        at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:781)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:981)
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     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)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:854) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481) 
        at com.example.phocast.MainActivity.onOptionsItemSelected(MainActivity.java:555) 
        at android.app.Activity.onMenuItemSelected(Activity.java:4137) 
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:383) 
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:219) 
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109) 
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109) 
        at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:64) 
        at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:207) 
        at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:781) 
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840) 
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158) 
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991) 
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:981) 
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625) 
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151) 
        at android.view.View.performClick(View.java:7125) 
        at android.view.View.performClickInternal(View.java:7102) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27336) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
2020-02-12 13:34:18.620 12562-12584/com.example.phocast I/example.phocas: Background young concurrent copying GC freed 110384(4259KB) AllocSpace objects, 46(1212KB) LOS objects, 41% free, 7792KB/12MB, paused 1.092ms total 150.548ms
2020-02-12 13:34:18.692 12562-12562/com.example.phocast I/Process: Sending signal. PID: 12562 SIG: 9
Snips answered 12/2, 2020 at 6:29 Comment(8)
Can you add details of com.google.android.material.card.MaterialCardView in your layout and also include the error log?Lucius
Hi, thanks for your quick replay. The error log is not very helpful, as it just says can not inflate binary xml file.Snips
Then can you add your full xml?Lucius
Post your full XML file and Build,gradle file here in : paste.ofcode.orgWinny
try to add and check : android:theme="@style/Theme.MaterialComponents.Light" in XML in com.google.android.material.card.MaterialCardView , material.io/develop/android/docs/getting-started and also https://mcmap.net/q/328805/-android-material-chip-component-crashing-app-unable-to-inflate-xmlWinny
Post your app theme and the full stacktrace.Oahu
@GabrieleMariotti my style (I guess by theme, you meant the style.xml), logcat and app level gradel added.Snips
@Snips The issue is reported in the stacktrace. Check the answer belowOahu
C
77

you can just keep it in your materialcardview itself also:

 <com.google.android.material.card.MaterialCardView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:cardCornerRadius="8dp"
     **** android:theme="@style/Theme.MaterialComponents.Light"  *****
      app:cardElevation="8dp">
Cableway answered 16/4, 2020 at 6:52 Comment(1)
Or you can use the desired solution for who's creating a new app as suggested in DeepLathia's answer.Tirade
O
30

The error is in the stacktrace:

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

With the version 1.1.0 you have to inherit from a Theme.MaterialComponents.

Change your app theme to

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  <!-- ....  -->
</style>
Oahu answered 12/2, 2020 at 8:19 Comment(2)
Thanks. This is working, except the fact that the active font color of tablayout has changed its colour (from white to violet), I guess as an artefact of transition from AppCompat to MaterialsTheme. I havn't set that font color explicitly. Given, this is beyond the scope of the current question, I will create a new issue. Will you mind if I tag that to you here?Snips
@BaRud. Thanks, no problem.Oahu
R
15

You need to change your AppTheme to have it's parent theme as a descendant of MaterialComponent

In your particular case changing the AppTheme from

  <style name="AppTheme" parent="Theme.AppCompat.DayNight">

to

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

should fix the issue.

Rahal answered 28/4, 2020 at 9:38 Comment(2)
Exactly! Forgot to change theme of new app ) thank youPonzo
Adding MaterialComponents in App theme solved my error thank you!Hertford
J
11

In my case I just added the android:theme="" attribute and it worked. My Material Card View element is as below -

<com.google.android.material.card.MaterialCardView
            android:id="@+id/edit_profile_card"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/dp_16"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_marginEnd="@dimen/dp_16"
            android:layout_marginBottom="@dimen/dp_10"
            **android:theme="@style/Theme.MaterialComponents.DayNight"**
            app:cardCornerRadius="@dimen/dp_10"
            app:cardElevation="@dimen/dp_20"
            app:state_dragged="false">

           ....
           ....

        </com.google.android.material.card.MaterialCardView>
Jillene answered 19/12, 2020 at 6:12 Comment(0)
Y
2

try latest version

implementation 'com.google.android.material:material:1.2.0-alpha03'

style.xml:

<style name="AppTheme" 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>
    </style>

in your xml:

<com.google.android.material.card.MaterialCardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</com.google.android.material.card.MaterialCardView>
Yves answered 12/2, 2020 at 6:36 Comment(0)
V
1

It works after changing this <style name="AppTheme" parent="Theme.AppCompat.DayNight> to <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

Vehicular answered 29/9, 2020 at 4:50 Comment(0)
R
1

Just change your theme in style.xml to:

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

it will work

Resistive answered 9/3, 2021 at 13:12 Comment(0)
P
1

For material3 style use android:theme="@style/Theme.Material3.DayNight". i.e.

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/edit_profile_card"*
        **style="@style/Widget.Material3.CardView.Outlined"**
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/dp_16"
        android:layout_marginTop="@dimen/dp_10"
        android:layout_marginEnd="@dimen/dp_16"
        android:layout_marginBottom="@dimen/dp_10"
        **android:theme="@style/Theme.Material3.DayNight"**
        app:cardCornerRadius="@dimen/dp_10"
        app:cardElevation="@dimen/dp_20"
        app:state_dragged="false">

       ....
       ....

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

or in theme use

<style name="Base.Theme.App" parent="Theme.Material3.DayNight">

or for NoActionBar case:

<style name="Base.Theme.App" parent="Theme.Material3.DayNight.NoActionBar">
Polyunsaturated answered 21/2, 2023 at 16:34 Comment(1)
that did the trick for meToyatoyama
D
0

If after changing the theme to Theme.MaterialComponents in the styles.xml didn't work then make sure you have added style tag in the MaterialCard.

style="@style/Widget.MaterialComponents.CardView"
Devinne answered 10/5, 2020 at 8:57 Comment(1)
i tried your answer i still get the same error. Is there any alternate solution?Raddi
K
0

If you want to keep the previous theme of your application (AppCompat) you can use a Bridge theme. They are described here

From the documentation:

Bridge themes inherit from AppCompat themes, but also define the new Material Components theme attributes for you. If you use a bridge theme, you can start using Material Design components without changing your app theme.

For instance in res/values/themes.xml:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>
Ko answered 19/4, 2022 at 18:17 Comment(0)
B
0

I have this error in newest material design version. I just remove inappropriate colors( app:strokeColor="?attr/colorSurfaceVariant") in resources and it works.

Butanone answered 13/6, 2022 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.