Espresso 中的 PreferenceMatchers

PreferenceMatchers in Espresso

我想点击我偏好的项目 activity。我试过下面的代码,

onData(withKey("preference-key")).perform(click());

但是它会抛出一个异常,比如

Caused by: android.support.test.espresso.AmbiguousViewMatcherException: Multiple Ambiguous Views found for matcher is assignable from class: class android.widget.AdapterView

我怎样才能正确地点击这个项目?

首先,如您所见,AmbiguousViewMatcherException 意味着您至少有两个具有相同 idkeytext、[=16= 的视图]等

你的代码告诉我你正在尝试编写 AdapterView 测试就像你有一个视图,所以你尝试像使用 onView 匹配器一样使用 onData 匹配器。抱歉,AdapterView 测试从未如此简单。

而不是

onData(withKey("preference-key")).perform(click());

像这样写代码

onData(anything())
  .inAdapterView(allOf(
      isDescendantOfA(withId(R.id.fragment1)), 
      withId(R.id.listview)))
    .atPosition(4)
    .perform(click());

在 Whosebug 上查找 AmbiguousViewMatcherException Espresso 问题。它经常退出问题,也许有人有像你这样的观点。