从 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)
那里的代码:
@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);
当我只是运行应用程序或使用 espresso 2.0 测试它时,它并没有在那里崩溃
最佳答案
这是一个奇怪的问题,所以我将首先给出一个对我有用的解决方案,然后尝试解释我认为正在发生的事情。
快速修复是在测试构建配置中重新声明您的支持库依赖项:
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 task: ./gradlew app:dependencies
来检查。不知何故,此依赖项优先于 compile 中声明的依赖项,并将其下拉到旧版本:+--- 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
关于android - NPE @ android.support.v7.widget.RecyclerView$LayoutManager.detachViewInternal,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30008836/