Java 爪哇和浓缩咖啡罐';t输入,需要支持输入方法或可从class:class SearchView分配

Java 爪哇和浓缩咖啡罐';t输入,需要支持输入方法或可从class:class SearchView分配,java,testing,android-espresso,Java,Testing,Android Espresso,我正在尝试使用Espresso fro测试具有以下xml的应用程序: <com.google.android.material.textfield.TextInputLayout android:id="@+id/tvLogin" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="my

我正在尝试使用Espresso fro测试具有以下xml的应用程序:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/tvLogin"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="myHint"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:imeOptions="flagNoExtractUi|actionNext"
        android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>
我得到这个错误:

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 displayed on the screen to the user) and (supports input methods or is assignable from class: class android.widget.SearchView))
Target view: "TextInputLayout{id=2131362315, res-name=tvLogin, visibility=VISIBLE, width=831, height=144, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@8241b55, tag=null, root-is-layout-requested=false, has-input-connection=false, x=39.0, y=39.0, child-count=2}"

您的
tvLogin
是一个
TextInputLayout
,它也是一个
LinearLayout
,这就是为什么在对其执行
typeText()
时会出现错误。由于
typeText()
仅适用于可编辑字段,并且
textinputtext
tvLogin
的后代,因此您可以重写测试以使用Espresso中的一种方便匹配器:

onView(allOf(supportsInputMethods(), isDescendantOfA(withId(R.id.tvLogin))))
    .perform(typeText("test"))
或者,您也可以为
textinputtext
提供id:

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/tvLoginEditText"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:imeOptions="flagNoExtractUi|actionNext"
    android:inputType="text" />
<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/tvLoginEditText"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:imeOptions="flagNoExtractUi|actionNext"
    android:inputType="text" />
onView(withId(R.id.tvLoginEditText))
    .perform(typeText("test"))