[英]NPE @ android.support.v7.widget.RecyclerView$LayoutManager.detachViewInternal
when switching from espresso 2.0 to 2.1 i am stumbling uppon this strange error: 当从espresso 2.0切换到2.1时,我发现了这个奇怪的错误:
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)
the code there: 那里的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.pass_list);
ButterKnife.inject(this);
AXT.at(openFileFAB).setVisibility(Build.VERSION.SDK_INT >= VERSION_STARTING_TO_SUPPORT_STORAGE_FRAMEWORK);
final LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
(!!here!!) recyclerView.setLayoutManager(llm);
It is not crashing there when I just run the app or test it with espresso 2.0 当我运行应用程序或使用espresso 2.0进行测试时,它不会崩溃
This is a strange issue, so I will give a resolution that worked for me first and then try to explain what I think is happening. 这是一个奇怪的问题,所以我先给出一个对我有用的决议,然后试着解释一下我的想法。
Quick fix is to redeclare your Support Library dependencies in test build configuration: 快速修复是在测试构建配置中重新声明支持库依赖项:
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'
This should fix your issue. 这应该可以解决您的问题。
The explanation is rather strange: looks like a espresso-contrib:2.1
depends on an older version of com.android.support:recyclerview-v7:22.0.0
. 解释相当奇怪:看起来像espresso-contrib:2.1
取决于旧版本的com.android.support:recyclerview-v7:22.0.0
。 This can be checked using Gradle app:dependencies task: ./gradlew app:dependencies
. 这可以使用Gradle app:dependencies task来检查: ./gradlew app:dependencies
。 Somehow this dependency takes precedence over the one declared in compile and pulls it down to the older version: 不知何故,这种依赖关系优先于在编译中声明的依赖关系,并将其拉低到旧版本:
+--- 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 (*)
This can be confirmed using app:dependencyInsight task: ./gradlew app:dependencyInsight --configuration androidTestCompile --dependency recyclerview-v7
这可以使用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
The result is that the test build uses different (order) version of recyclerview-v7
that turns out to be buggy. 结果是测试版本使用不同(订单)版本的recyclerview-v7
,结果证明是错误的。 Adding androidTestCompile helps Gradle detect the conflict and resolve it properly. 添加androidTestCompile有助于Gradle检测冲突并正确解决。 After the fix: 修复后:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.