18

私は 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");
    }

コードを変更する方法についての提案は高く評価されます

4

2 に答える 2

27
@UiThreadTest
public void yourMethod() {

@UiThreadTest アノテーションは、UI スレッドで実行されるようにこのメソッドをビルドするよう Android に指示します。これにより、メソッドは、テスト中のアプリケーションでスピナー ウィジェットの状態を変更できます。この @UiThreadTest の使用は、必要に応じて UI スレッドでメソッド全体を実行できることを示しています。

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

于 2011-11-14T10:19:04.033 に答える
7

非 UI スレッドから UI を更新しようとしていると思います。そのためには、コードを .xml 内に配置する必要がありますrunOnUiThread()

ActivityName.this.runOnUiThread(new Runnable() {

            public void run() {
                // your code to update the UI
            }
        });
于 2011-11-14T10:22:41.870 に答える