ligi
ligi

Reputation: 39539

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

when switching from espresso 2.0 to 2.1 i am stumbling uppon this strange error:

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

Upvotes: 4

Views: 454

Answers (1)

atok
atok

Reputation: 5948

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. This can be checked using 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

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. Adding androidTestCompile helps Gradle detect the conflict and resolve it properly. 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

Upvotes: 4

Related Questions