Espresso onData在视图上执行“加载适配器数据”时出错

我有一个应用程序,有ListView,我想找到LinearLayout,id = order_untake_jijia_listview_jia

在此处输入图像描述

代码是:

onData(withClassName(endsWith("ListView"))) .inAdapterView(allOf(withId(R.id.order_untake_jijia_listview_ll), hasSibling(withText("9.0")))) .atPosition(0).onChildView(withId(R.id.order_untake_jijia_listview_jia)); dataInteraction.perform(ViewActions.click()); 

但我有错误:

 Error performing 'load adapter data' on view '(with id: com.edaixi:id/order_untake_jijia_listview_ll and has sibling: with text: is "9.0")'. Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: (is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user) Target view: "LinearLayout{id=2131493753, res-name=order_untake_jijia_listview_ll, visibility=VISIBLE, width=170, height=50, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=572.0, y=25.0, child-count=3}" 

您正在错误地使用onDataonData采用对象匹配器 – 它旨在匹配用于填充列表的自定义模型对象。 因此,如果你有一个简单的字符串列表,你会说onData(instanceOf(String.class)).atPosition(0)onData(is("This exact String at position 0"))

inAdapterView用于匹配包含数据的特定适配器视图实例。 当您需要在屏幕上显示多个列表时,您需要这样做。 例如,如果你有一个名为R.id.list1 ListView和第二个包含StringR.id.list2 ,你会说onData(is("The String")).inAdapterView(withId(R.id.list1))匹配第一个列表中的对象。

如果只需要单击适配器中的单个项目,通常也不需要在这些情况下指定子视图。 您通常使用onChildView对子视图项执行断言,或者如果您碰巧在该子视图上有单独的专用单击侦听器。

因此,似乎您的情况过于复杂,您应该能够将代码更新为以下内容:

 onData(instanceOf(MyCustomClassOfObjectsInAdapter.java)) .atPosition(0) .perform(click()); 

如果这不能解决您的问题,请提供一些列表项目的布局代码以及包含listview的主屏幕布局,以及带有显示示例的屏幕截图,并阐明您要尝试的内容match / act / assert,我可以尝试引导您完成所需的正确方法集。

希望有所帮助!