加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:34:16 所属栏目:Java 来源:网络整理
导读:我有一个应用程序,有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")))) .atPo
我有一个应用程序,有ListView,我想找到LinearLayout,id = order_untake_jijia_listview_jia

enter image description here

代码是:

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

解决方法

您正在错误地使用onData.
onData采用对象匹配器 – 它旨在匹配用于填充列表的自定义模型对象.因此,如果你有一个简单的字符串列表,你会说onData(instanceOf(String.class)).atPosition(0)或onData(是(“这是位于0的精确字符串”)).

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

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

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

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

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

希望有所帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读