【问题标题】:InstrumentTest Resources with Android + Gradle使用 Android + Gradle 的 InstrumentTest 资源
【发布时间】:2013-12-06 13:39:14
【问题描述】:

我需要测试一个自定义适配器,它使用 TypedArray 来创建数据。我想为它编写一个仪器测试(即 AndroidTestCase),这当然需要它自己的测试资源。

使用以下结构设置 instrumentTest 子文件夹后:

/ instrumentTest
  - src
  / res
    - drawable
    - layout
      # layouts for the tests
    - values
      # resource values where the TypedArray exists

最初,Android Studio (IntelliJ IDEA 13) 无法识别 R 引用(在PACKAGE_NAME.test.R 下,但在我运行assembleTest 之后,它们会被正确识别。当我执行connectedInstrumentTest 时,测试会因为这个错误而失败:

android.content.res.Resources$NotFoundException: Array resource ID #0x7f040000
    at android.content.res.Resources.obtainTypedArray(Resources.java:509)

失败的资源位于PACKAGE_NAME.test.R 下。

帮助?

【问题讨论】:

    标签: android intellij-idea android-resources android-gradle-plugin


    【解决方案1】:

    问题出在AndroidTestCase。它提供的上下文似乎实际上并不可用。更改为InstrumentationTestCase 并从中获取资源

    getInstrumentation().getContext().getResources()
    

    似乎工作得很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多