NPE @ android.support.v7.widget.RecyclerView $ LayoutManager.detachViewInternal

时间:2015-05-03 00:02:25

标签: android android-espresso

从espresso 2.0切换到2.1时,我发现了这个奇怪的错误:

ON DELETE CASCADE

那里的代码:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ChildHelper.detachViewFromParent(int)' on a null object reference
    at android.support.v7.widget.RecyclerView$LayoutManager.detachViewInternal(RecyclerView.java:5407)
    at android.support.v7.widget.RecyclerView$LayoutManager.detachViewAt(RecyclerView.java:5400)
    at android.support.v7.widget.RecyclerView.setAdapterInternal(RecyclerView.java:647)
    at android.support.v7.widget.RecyclerView.swapAdapter(RecyclerView.java:594)
    at org.ligi.passandroid.ui.PassListActivity.onCreate(PassListActivity.java:161)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:489)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

当我运行应用程序或使用espresso 2.0进行测试时,它并没有崩溃。

1 个答案:

答案 0 :(得分:4)

这是一个奇怪的问题,所以我会首先给出一个对我有用的决议,然后尝试解释我的想法。

快速修复是在测试构建配置中重新声明支持库依赖项:

androidTestCompile 'com.android.support:support-v4:22.1.1'
androidTestCompile 'com.android.support:appcompat-v7:22.1.1'
androidTestCompile 'com.android.support:cardview-v7:22.1.1'
androidTestCompile 'com.android.support:gridlayout-v7:22.1.1'
androidTestCompile 'com.android.support:recyclerview-v7:22.1.1'

这应该可以解决您的问题。

解释相当奇怪:看起来像espresso-contrib:2.1取决于较早版本的com.android.support:recyclerview-v7:22.0.0。可以使用Gradle app:dependencies 任务./gradlew app:dependencies来检查。不知何故,这种依赖关系优先于在编译中声明的依赖关系,并将其拉低到旧版本:

+--- com.android.support.test.espresso:espresso-contrib:2.1
|    +--- com.android.support:recyclerview-v7:22.0.0
|    |    \--- com.android.support:support-v4:22.0.0
|    +--- com.android.support:support-v4:22.0.0
|    \--- com.android.support.test.espresso:espresso-core:2.1 (*)

可以使用 app:dependencyInsight 任务确认: ./gradlew app:dependencyInsight --configuration androidTestCompile --dependency recyclerview-v7

com.android.support:recyclerview-v7:22.0.0
\--- com.android.support.test.espresso:espresso-contrib:2.1
     \--- androidTestCompile

结果是测试版本使用了recyclerview-v7的不同(订单)版本,结果证明是错误的。添加androidTestCompile有助于Gradle检测冲突并正确解决。修复后:

com.android.support:recyclerview-v7:22.1.1 (conflict resolution)
\--- androidTestCompile

com.android.support:recyclerview-v7:22.0.0 -> 22.1.1
\--- com.android.support.test.espresso:espresso-contrib:2.1
     \--- androidTestCompile