【问题标题】:Unable to inflate MaterialCardVIew class in Instrumental test无法在仪器测试中膨胀 MaterialCardVIew 类
【发布时间】:2019-10-30 16:39:00
【问题描述】:

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

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

【问题讨论】:

标签: android material-design android-espresso materialcardview


【解决方案1】:

将您的主题父母更改为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>

【讨论】:

    【解决方案2】:

    就我而言,我需要改变

    adapter = new CustomersListAdapter(mContext, customers);
    

    adapter = new CustomersListAdapter(this, customers);
    

    我将 添加到列表项。然后我得到了错误..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 2017-12-19
      • 2017-02-24
      • 2020-11-26
      相关资源
      最近更新 更多