无法在乐器测试中扩充MaterialCardVIew类

时间:2019-06-17 05:45:30

标签: android material-design android-espresso materialcardview

我正在尝试测试自己的recyclerView,并且我正在使用物料卡视图进行项目显示,并且在应用正常运行的同时,在尝试测试时出现此错误:

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

现在测试非常简单:

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

睡眠只是让应用程序等待尝试显示列表。 而奇怪的是,当我不在列表项布局中使用materialCardView时,测试通过。 现在,我将应用主题更改为:

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

但是测试仍然消失,那么如何更改应用程序的测试主题?

2 个答案:

答案 0 :(得分:2)

将您的主题父母更改为Theme.MaterialComponents或在下面复制

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

答案 1 :(得分:0)

就我而言,我需要改变

adapter = new CustomersListAdapter(mContext, customers);

adapter = new CustomersListAdapter(this, customers);

我添加了 到列表项。然后我出错了..