android – (Robotium)如何在RadioGroup中选择RadioButton

在我的
Android应用视图布局中,我有一个< RadioGroup>其中包含两个< RadioButton>:

<RadioGroup
     android:id="@+id/my_radio_group"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical">

      <RadioButton 
       android:id="@+id/yes_btn"

       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/yes"
      />

     <RadioButton
       android:id="@+id/no_btn" 

       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/no"
      />
</RadioGroup>

我使用Robotium库为这个无线电组写一个JUnit测试来选择一个单选按钮.测试代码段是:

Solo solo = new Solo(getInstrumentation(), getActivity()); 
...
solo.clickOnRadioButton(R.id.yes_btn); //I expect the "yes" radio button will be selected

我希望上面的测试代码会选择“YES”单选按钮,但运行它时会引发错误:

junit.framework.AssertionFailedError: 2131427675 RadioButtons are not found!
at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:417)
at com.jayway.android.robotium.solo.Clicker.clickOn(Clicker.java:374)
at com.jayway.android.robotium.solo.Solo.clickOnRadioButton(Solo.java:1063)
...

如何选择一个< RadioButton>在< RadioGroup>中那么Robotium ??

最佳答案 试试下面的代码

RadioButton rb = (RadioButton) solo.getView(R.id.yes_btn);
solo.clickOnView(rb);

当您传递资源ID时,solo.clickOnRadioButton()将视图索引作为参数.

点赞