Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试单击图标(文本视图)时出现Espresso AmbiguousViewMatcher异常_Android_Android Espresso_Android Espresso Recorder - Fatal编程技术网

Android 尝试单击图标(文本视图)时出现Espresso AmbiguousViewMatcher异常

Android 尝试单击图标(文本视图)时出现Espresso AmbiguousViewMatcher异常,android,android-espresso,android-espresso-recorder,Android,Android Espresso,Android Espresso Recorder,LinearLayout(productList)在运行时动态填充子视图,如下所示: @ViewById(R.id.ll_products) LinearLayout productList; public void createProductList() { productList.addView(getView(mobilePhone)) productList.addView(getView(internet)) productList.addView(getVi

LinearLayout(productList)在运行时动态填充子视图,如下所示:

@ViewById(R.id.ll_products)
LinearLayout productList;

public void createProductList() {
    productList.addView(getView(mobilePhone))
    productList.addView(getView(internet))
    productList.addView(getView(television))
}

public View getView(Product product) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView layout = (LinearLayout) inflater.inflate(R.layout.row_product, null);
    TextView productIcon = (TextView) layout.findViewById(R.id.tv_product_row_icon);
    productIcon.setText(product.getProductIcon());
    productName.setText(product.getName());
}
productList故意采用线性布局,而不是ListView

产品列表有三个产品-每个产品都有图标(可以复制)

我想记录一个场景,在该场景中,我单击第二个产品的图标。 这样的场景如何避免含糊不清的ViewMatcherException

不幸的是,以下代码将不起作用-将找到三个R.id.tv\u产品\u行\u图标

    ViewInteraction appCompatTextView = onView(withId(R.id.tv_product_row_icon));
    appCompatTextView.perform(scrollTo(), click());

如何指定要单击的第二个图标?

不幸的是,您必须为这种情况创建自定义图标

以下内容将
视图与指定索引相匹配:

public static Matcher<View> withIndex(final Matcher<View> matcher, final int index) {
    return new TypeSafeMatcher<View>() {
        int currentIndex = 0;

        @Override
        public void describeTo(Description description) {
            description.appendText("with index: ");
            description.appendValue(index);
            matcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            return matcher.matches(view) && currentIndex++ == index;
        }
    };
}
带索引的公共静态匹配器(final Matcher Matcher,final int index){
返回新的TypeSafeMatcher(){
int currentIndex=0;
@凌驾
公共无效说明(说明){
description.appendText(“带索引:”);
说明.附加值(索引);
匹配器描述(描述);
}
@凌驾
公共布尔匹配安全(视图){
返回matcher.matches(视图)&¤tIndex++==index;
}
};
}
适用于您的情况的用法:

Matcher<View> secondIconMatcher = allOf(withId(R.id.tv_product_row_icon));
onView(withIndex(secondIconMatcher , 1)).perform(click());
Matcher secondIconMatcher=allOf(带id(R.id.tv\u product\u row\u图标));
onView(带索引(secondIconMatcher,1))。执行(单击());

不幸的是,您必须为这种情况创建自定义

以下内容将
视图与指定索引相匹配:

public static Matcher<View> withIndex(final Matcher<View> matcher, final int index) {
    return new TypeSafeMatcher<View>() {
        int currentIndex = 0;

        @Override
        public void describeTo(Description description) {
            description.appendText("with index: ");
            description.appendValue(index);
            matcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            return matcher.matches(view) && currentIndex++ == index;
        }
    };
}
带索引的公共静态匹配器(final Matcher Matcher,final int index){
返回新的TypeSafeMatcher(){
int currentIndex=0;
@凌驾
公共无效说明(说明){
description.appendText(“带索引:”);
说明.附加值(索引);
匹配器描述(描述);
}
@凌驾
公共布尔匹配安全(视图){
返回matcher.matches(视图)&¤tIndex++==index;
}
};
}
适用于您的情况的用法:

Matcher<View> secondIconMatcher = allOf(withId(R.id.tv_product_row_icon));
onView(withIndex(secondIconMatcher , 1)).perform(click());
Matcher secondIconMatcher=allOf(带id(R.id.tv\u product\u row\u图标));
onView(带索引(secondIconMatcher,1))。执行(单击());

它工作得很好!谢谢然而,我有一个例子,我有5个图标-最后一个图标只有在滚动之后才在屏幕上可见。当我使用您的解决方案时,我得到一个异常-NoMatchingViewException:在层次结构中找不到与索引匹配的视图:。你知道是什么原因造成的吗?你自己回答了问题-视图在屏幕上不可见。解决方案是在视图可见时进行匹配:
onView(matcher).check(matches(isDisplayed())内部try catch块。如果抛出异常,那么应该滚动父视图。它工作得很好!谢谢然而,我有一个例子,我有5个图标-最后一个图标只有在滚动之后才在屏幕上可见。当我使用您的解决方案时,我得到一个异常-NoMatchingViewException:在层次结构中找不到与索引匹配的视图:。你知道是什么原因造成的吗?你自己回答了问题-视图在屏幕上不可见。解决方案是在视图可见时进行匹配:
onView(matcher).check(matches(isDisplayed())内部try catch块。如果抛出异常,则应滚动父视图。