Android Robotium 测试,运行时异常无法从主应用程序线程调用此方法

Android Robotium Testing, Runtime Exception This method can not be called from the main application thread

所以我正在使用 Robotium 测试我的 android 应用程序,当我通过 solo.clickInList(0,1); 单击列表中的项目时,它会给我

java.lang.RuntimeException: This method can not be called from the main application thread
at android.app.Instrumentation.validateNotAppThread(Instrumentation.java:1787)
at android.app.Instrumentation.runOnMainSync(Instrumentation.java:348)
at com.robotium.solo.Scroller.scrollListToLine(Scroller.java:326)
at com.robotium.solo.Scroller.scrollList(Scroller.java:276)
at com.robotium.solo.Scroller.scroll(Scroller.java:195)
at com.robotium.solo.Scroller.scroll(Scroller.java:156)
at com.robotium.solo.Scroller.scrollDown(Scroller.java:169)
at com.robotium.solo.Waiter.waitForView(Waiter.java:154)
at com.robotium.solo.Waiter.waitForAndGetView(Waiter.java:521)
at com.robotium.solo.Clicker.clickInList(Clicker.java:516)
at com.robotium.solo.Solo.clickInList(Solo.java:1244)
at com.teamname.tutortrader.AvailableSessionsActivityTest.testViewOneSession(AvailableSessionsActivityTest.java:119)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.access[=11=]0(InstrumentationTestCase.java:36)
at android.test.InstrumentationTestCase.run(InstrumentationTestCase.java:189)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1855)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我在我的函数之上使用 @UiThreadTest

正如我在这里发现的那样,它看起来像是 robotium 中的一个错误。 。不用 robotium,你自己也可以做同样的事情。

public void onListViewItemClick(){ Activity activity = getActivity(); final ListView listView = (ListView)activity.findViewById(android.R.id.list); getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { listView.performItemClick(listView.getAdapter().getView(0, null, null), 0, listView.getItemIdAtPosition(0)); } }); }