Robolectric 3.0:我如何解决InflateException“错误膨胀类RecyclerView”

时间:2015-02-03 18:47:49

标签: android robolectric

更新: Robolectric 3.0决赛已经发布,这应该不再是问题......)

我已经cloned了Robolectric deckard-gradle样本并进行了以下更改:

  • 更改为robolectric:来自Sonateype的3.0-SNAPSHOT。
  • added a RecyclerView到deckard.xml布局文件。

正如预期的那样,这会导致以下异常:

android.view.InflateException: XML file /private/tmp/deckard-gradle/build/intermediates/res/debug/layout/deckard.xml line #-1 (sorry, not yet implemented): Error inflating class RecyclerView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    at android.app.Activity.setContentView(Activity.java:1895)
    at com.example.activity.DeckardActivity.onCreate(DeckardActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:5133)
    at org.robolectric.util.ReflectionHelpers$3.run(ReflectionHelpers.java:144)
    at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:241)
    at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:138)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:114)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:309)
    at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:47)
    at org.robolectric.util.ActivityController.create(ActivityController.java:110)
    at org.robolectric.util.ActivityController.create(ActivityController.java:121)
    at com.example.activity.DeckardActivityTest.testSomething(DeckardActivityTest.java:17)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:167)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: android.view.RecyclerView
    at org.robolectric.internal.bytecode.InstrumentingClassLoader.getByteCode(InstrumentingClassLoader.java:166)
    at org.robolectric.internal.bytecode.InstrumentingClassLoader.findClass(InstrumentingClassLoader.java:123)
    at org.robolectric.internal.bytecode.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:95)
    at android.view.LayoutInflater.createView(LayoutInflater.java:559)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
    at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:48)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
    ... 37 more

这当然是因为Robolectric还不支持像RecyclerView这样的appcompat-v7。

但是,必须有一个解决办法来避免InflateExceoption?我现在甚至不关心RecyclerView是否被ShadowRecyclerView充气。我只是想避免测试中的异常,不用攻击我的生产代码,比如注入要使用的布局ID ...

我认为这可以通过实施Shadow RecyclerView来解决,但到目前为止我还没有成功。

我正在使用Java 1.7.0_60和最新版本的Android工具。

在Roithlectric的github上记录相关问题:

1 个答案:

答案 0 :(得分:0)

此问题在Robolectric 3中已解决。