问题
I have the following test written for RecyclerView
@Test
fun testHomeActivity_recyclerViewIndex0_configurationChange_displaysWelcomeMessageCorrectly() {
launch(HomeActivity::class.java).use {
onView(isRoot()).perform(orientationLandscape())
onView(
atPositionOnView(R.id.home_recycler_view, 0, R.id.welcome_text_view )
).check(matches(withText(containsString("Welcome"))))
}
}
It gets passed successfully on my real device, but gets failed on Nexus S
hdpi emulator device. With the following error:
kotlin.KotlinNullPointerException
at org.oppia.app.recyclerview.RecyclerViewMatcher$Companion$atPositionOnView$1.matchesSafely(RecyclerViewMatcher.kt:52)
at org.oppia.app.recyclerview.RecyclerViewMatcher$Companion$atPositionOnView$1.matchesSafely(RecyclerViewMatcher.kt:28)
at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:65)
at androidx.test.espresso.base.ViewFinderImpl$MatcherPredicateAdapter.apply(ViewFinderImpl.java:130)
at androidx.test.espresso.core.internal.deps.guava.collect.Iterators$5.computeNext(Iterators.java:637)
at androidx.test.espresso.core.internal.deps.guava.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:141)
at androidx.test.espresso.core.internal.deps.guava.collect.AbstractIterator.hasNext(AbstractIterator.java:136)
at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:69)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:280)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:272)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I don't think it really concerns the issue but here is home_recycler_view.xml :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="org.oppia.app.R" />
<variable
name="presenter"
type="org.oppia.app.home.HomeFragmentPresenter" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/home_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:overScrollMode="never"
android:paddingTop="36dp"
android:paddingBottom="@dimen/bottom_white_space"
android:scrollbars="none" />
</LinearLayout>
</layout>
I have tried looking for it everywhere like here and here
回答1:
Ok I solved the issue. Basically, on orientation change recyclerview scrolls up a little bit so the view I am trying to find gets hidden.
I added this line to scroll to that position, and then check the text.
onView(withId(R.id.home_recycler_view)).perform(scrollToPosition<RecyclerView.ViewHolder>(0))
来源:https://stackoverflow.com/questions/60995054/nullpointerexception-in-recyclerview-espresso-test-on-hdpi-device