4

I am trying to test my recyclerView which and I'm using material card view for items display, and while the app works fine, i get this error when I'm trying to test:

android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

now the test is as simple as it gets:

  @Test
    fun shouldShowList() {
        launchFragmentInContainer<PostsFragment>()
        Thread.sleep(5000)
    }

the sleep is only for the app to wait to try to show the list. and the strange part is when I don't use materialCardView for my list item layout, the test passes. Now I have changed my app theme to this:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

but the test still fades, so How can I change the test theme of my app?

Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38
  • Check this out. https://stackoverflow.com/questions/32346748/robolectric-illegalstateexception-you-need-to-use-a-theme-appcompat-theme-or – Mike S Jul 03 '19 at 23:18

3 Answers3

5

Change your Theme parents to Theme.MaterialComponents or copy below

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.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" 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>
    </style>
James Christian Kaguo
  • 1,251
  • 15
  • 14
1

You will need to add the theme in the launchFragmentInContainer<PostsFragment>() as launchFragmentInContainer<PostsFragment>(themeResId = R.style.Theme_Mytheme).

As you are launching the fragment as a standalone i.e. without a Application it doesnot know the application theme so using any Material component will cause it to crash thus need to pass the theme to the launchFragmentInContainer

Sayok Majumder
  • 1,012
  • 13
  • 28
-1

in my case I needed to change

adapter = new CustomersListAdapter(mContext, customers);

to

adapter = new CustomersListAdapter(this, customers);

I added the <com.google.android.material.card.MaterialCardView> to list item. Then I got error..