gpt4 book ai didi

Android Junit 测试失败,出现 "Only the original thread that created a view hierarchy can touch its views."

转载 作者:IT老高 更新时间:2023-10-28 23:30:28 25 4
gpt4 key购买 nike

我对 Android 非常陌生,正在使用 Robotium 编写一些基本的 Android 测试,但失败并出现异常

"android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."

下面是基本的测试用例描述:-

测试用例:-

public void testSearch() {
Activity a = getActivity();
SearchItemActivity search = new SearchItemActivity(solo);
search.searchText("ipod", a);

}

SearchItemActivity.searchText(String) is defined as

public void searchText(final String search, Activity act) {
Button v = (Button) act
.findViewById(com.test.mobile.R.id.text_search_field);
((Button) v).setText("");
((Button) v).setText(search);
solo.sendKey(Solo.ENTER);
solo.waitForActivity("FoundItemdDetailActivity");
solo.assertCurrentActivity("Expected FoundItemDetail activity","FoundItemdDetailActivity");
}

任何关于如何修改我的代码的建议都将不胜感激

最佳答案

@UiThreadTest
public void yourMethod() {

@UiThreadTest 注释告诉 Android 构建此方法,以便它在 UI 线程上运行。这允许该方法在被测应用程序中更改微调器小部件的状态。 @UiThreadTest 的这种使用表明,如有必要,您可以在 UI 线程上运行整个方法。

http://developer.android.com/tools/testing/activity_test.html

关于Android Junit 测试失败,出现 "Only the original thread that created a view hierarchy can touch its views.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8017443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com