首页 > 解决方案 > Android:布局的任何视图上的 androidx.test.espresso.AmbiguousViewMatcherException

问题描述

在我的浓缩咖啡测试用例检查

onView(withId(R.id.myRecyclerView)).check(matches(isDisplayed()))

给出异常 androidx.test.espresso.AmbiguousViewMatcherException: 'with id: com.xxx.xxx.debug:id/myRecyclerView' 匹配层次结构中的多个视图。

如何在层次结构中有多个 recyclerview 实例。

布局结构为:

`<androidx.core.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:fillViewport="false"
            android:scrollbars="none"
            android:visibility="condition here"
            app:layout_constraintBottom_toTopOf="@id/bottomButtonsContainer"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <FrameLayout
                android:id="@+id/contents"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/lead_details_spacing"
                android:orientation="vertical"
                android:paddingBottom="@dimen/lead_details_spacing">

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/myRecyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:overScrollMode="never"
                        android:contentDescription="test"
                        android:visibility="condition"
                        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"></androidx.recyclerview.widget.RecyclerView>

            </FrameLayout>

        </androidx.core.widget.NestedScrollView>

如何解决此异常?

编辑

我浏览了登录详细信息。我看到的是在视图层次结构下

Problem views are marked with ‘****MATCHES****’ below. View Hierarchy:整个布局来了两次。

流程:主页 -> 选项卡活动 -> tab2 上面提到的带有 recyclerview 的布局 -> 单击此布局的按钮会转到另一个活动 X -> 返回 tab2 -> 使用 espresso 验证 recyclerview 的内容

当我第一次登陆 tab2 片段时,我可以单击按钮。但是当我从活动 X 回到 tab2 并尝试再次单击时,我得到了相同的 AmbiguousViewMatcher 异常。所以整个布局不知何故被复制了。我尝试检查布局,但没有看到任何重复项。关于可能出错的任何指示?

标签: androidandroid-recyclerviewandroid-espresso

解决方案


推荐阅读