我遇到了 Robotium 的问题,当加载活动时 waitForActivity 完成,但它的视图尚无法访问。例如:
solo.clickOnButton("Go");
assertTrue(solo.waitForActivity("ActivityTest", 5000));
Activity a = solo.getCurrentActivity(); // This works - a is "ActivityTest"
ArrayList<View> v = solo.getViews(); // This doesn't work - v is empty
问题似乎是活动尚未完全加载;睡眠延迟解决了这个问题:
solo.clickOnButton("Go");
assertTrue(solo.waitForActivity("ActivityTest", 5000));
Activity a = solo.getCurrentActivity(); // This works - a is "ActivityTest"
solo.sleep(5000);
ArrayList<View> v = solo.getViews(); // This works - v has lots of views
这只是 Robotium 的生活事实还是我做错了?
编辑:这是我现在用来代替solo.waitForActivity的:
public Boolean waitForActivity(String name) {
int timeout = 10 * 1000;
long start = Calendar.getInstance().getTimeInMillis();
assertTrue(solo.waitForActivity(name, timeout));
solo.assertCurrentActivity(name, name);
ArrayList<View> views = solo.getViews();
while (views.isEmpty()) {
solo.sleep(1000);
views = solo.getViews();
if (Calendar.getInstance().getTimeInMillis() > start + timeout)
return false;
}
return true;
}