3

我正在尝试测试我的 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">

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

4

2 回答 2

5

将您的主题父母更改为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>
于 2020-03-01T18:45:50.237 回答
-1

就我而言,我需要改变

adapter = new CustomersListAdapter(mContext, customers);

adapter = new CustomersListAdapter(this, customers);

我将 <com.google.android.material.card.MaterialCardView> 添加到列表项。然后我得到错误..

于 2021-01-16T18:49:03.737 回答