设备上RecyclerView浓咖啡测试中的NullPointe

设备上RecyclerView浓咖啡测试中的NullPointe

本文介绍了Hdpi设备上RecyclerView浓咖啡测试中的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为RecyclerView编写了以下测试

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"))))
    }
  }

它在我的真实设备上成功通过,但是在Nexus S hdpi仿真器设备上失败.出现以下错误:

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)

我不认为这确实与问题有关,但这是home_recycler_view.xml:

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

推荐答案

好,我解决了这个问题.基本上,在方向更改时,recyclerview会向上滚动一点,以便隐藏我要查找的视图.我添加了此行以滚动到该位置,然后检查文本.onView(withId(R.id.home_recycler_view)).perform(scrollToPosition<RecyclerView.ViewHolder>(0))

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))

这篇关于Hdpi设备上RecyclerView浓咖啡测试中的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:11