【问题标题】:Android Espresso: unable to find CheckBoxPreference with unique android:idAndroid Espresso:无法找到具有唯一 android:id 的 CheckBoxPreference
【发布时间】:2016-10-22 09:21:45
【问题描述】:

我正在尝试测试 PreferenceScreen 上的复选框。

PreferenceScreen 包含两个 CheckBoxPreferences,每个都有唯一的 android:id。

    <CheckBoxPreference
        android:id="@+id/first_checkbox"
        android:key="first_checkbox"
        android:title="First checkbox" />

    <CheckBoxPreference
        android:id="@+id/second_checkbox"
        android:key="second_checkbox"
        android:title="Second checkbox" />

以下是 R.java 文件中的 id 值:

public static final class id {
    public static final int first_checkbox=0x7f0a0000;
    public static final int second_checkbox=0x7f0a0001;
}

在我的测试中:

    ViewInteraction cbxFirst = onView(withId(R.id.first_checkbox));

我明白了:

    NoMatchingViewException: No views in  hierarchy found matching: with id:  
    com.test.fragmentpreference:id/first_checkbox

当我尝试通过“android.R.id.checkbox”而不是“R.id.first_checkbox”进行搜索时:

    ViewInteraction cbxFirst = onView(withId(android.R.id.checkbox));

我收到:

    AmbiguousViewMatcherException: 'with id: android:id/checkbox' matches multiple views in the hierarchy.

我的问题是:如何使用 'first_checkbox' id 测试第一个 CheckBoxPreference ?

【问题讨论】:

  • onView(withText("First checkbox"));之类的文字访问它怎么样?
  • @Kamran Ahmed 我可以使用 'onView(withText("First checkbox"));' 访问它但我的目标是了解如何使用唯一的 android:id 访问特定视图,因为在许多情况下,当您有许多文本略有不同的视图时,使用“withText()”更容易出错。
  • 我同意,我只是想更多地了解这个问题。在正常情况下它应该可以工作......我不确定CheckBoxPreference 的工作方式是否不同,理想情况下应该不会。

标签: android checkbox android-espresso checkboxpreference


【解决方案1】:

根据https://developer.android.com/reference/android/preference/CheckBoxPreference.html

“android:id”不是 CheckBoxPreference 允许的 xml 属性。

在这种情况下,我错误地将“android:id”添加到了preference.xml 中的CheckBoxPreference 声明中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2015-05-17
    • 2011-10-23
    • 2011-10-29
    相关资源
    最近更新 更多