Annotations have nothing to do with this, waitForIdleSync() has nothing to do with this, it's just a basic aspect of Android that you can only call on to UI objects from the thread running the UI. Use this:
http://developer.android.com/reference/android/app/Instrumentation.html#runOnMainSync%28java.lang.Runnable%29 On Tue, Aug 25, 2009 at 12:43 AM, [email protected] < [email protected]> wrote: > > I have found the root cause of this problem. > That's because of getInstrumentation().waitForIdleSync() > I have seen someone else use waitForIdleSync() after UI action, I want > to know if it is necessary, and in which case we need to call this > method? > If we want to call it, how do we call it correctly? > > On Aug 25, 3:08 pm, "[email protected]" > <[email protected]> wrote: > > Hi Dianne, > > I have tried to annotate the test method with UiThreadTest, but still > > it doesn't work, the trace log is: > > java.lang.RuntimeException: This method can not be called from the > > main application thread > > at android.app.Instrumentation.validateNotAppThread > > (Instrumentation.java:1500) > > at android.app.Instrumentation.waitForIdleSync(Instrumentation.java: > > 397) > > at com.liufeng.vehicle.test.BikeTests.testBikeButton(BikeTests.java: > > 45) > > at java.lang.reflect.Method.invokeNative(Native Method) > > at android.test.InstrumentationTestCase.runMethod > > (InstrumentationTestCase.java:191) > > at android.test.InstrumentationTestCase.access$000 > > (InstrumentationTestCase.java:36) > > at android.test.InstrumentationTestCase$2.run > > (InstrumentationTestCase.java:171) > > at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java: > > 1538) > > at android.os.Handler.handleCallback(Handler.java:587) > > at android.os.Handler.dispatchMessage(Handler.java:92) > > at android.os.Looper.loop(Looper.java:123) > > at android.app.ActivityThread.main(ActivityThread.java:3948) > > at java.lang.reflect.Method.invokeNative(Native Method) > > at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run > > (ZygoteInit.java:782) > > at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) > > at dalvik.system.NativeStart.main(Native Method) > > > > So what's the problem? And is there any other method I can use to > > perform a call on UI thread? > > > > On Aug 25, 9:40 am, Dianne Hackborn <[email protected]> wrote: > > > > > > > > > As the error says, you are calling peformClick() for a different thread > than > > > the UI thread (here the instrumentation thread). You can use the > > > Instrumentation API to perform a call on the UI thread. > > > > > On Mon, Aug 24, 2009 at 6:08 PM, [email protected] < > > > > > [email protected]> wrote: > > > > > > no one know it? > > > > > > On Aug 24, 11:01 am, "[email protected]" > > > > <[email protected]> wrote: > > > > > I am using ActivityInstrumentationTestCase2 to do some test for an > > > > > activity. > > > > > I get a button in the setUp() method like this: > > > > > protected void setUp() throws Exception { > > > > > super.setUp(); > > > > > act = getActivity(); > > > > > ........ > > > > > btn = (Button)act.findViewById( R.id.bike_button ); // this > > > > > button has been defined in layout > > > > > } > > > > > Then I use this button to perform a click in a test method like > this: > > > > > public void testBikeButton(){ > > > > > //click the bike button > > > > > btn.performClick(); > > > > > ......} > > > > > > > When run it as android junit, there will be an error like this: > > > > > "android.view.ViewRoot$CalledFromWrongThreadException: Only the > > > > > original thread that created a view hierarchy can touch its views. > > > > > at android.view.ViewRoot.checkThread(ViewRoot.java:2440) > > > > > at android.view.ViewRoot.playSoundEffect(ViewRoot.java:2261) > > > > > at android.view.View.playSoundEffect(View.java:7457) > > > > > at android.view.View.performClick(View.java:2178) > > > > > at > com.liufeng.vehicle.test.BikeTests.testBikeButton(BikeTests.java: > > > > > 40) > > > > > at java.lang.reflect.Method.invokeNative(Native Method) > > > > > at android.test.InstrumentationTestCase.runMethod > > > > > (InstrumentationTestCase.java:191) > > > > > at android.test.InstrumentationTestCase.runTest > > > > > (InstrumentationTestCase.java:181) > > > > > at android.test.ActivityInstrumentationTestCase2.runTest > > > > > (ActivityInstrumentationTestCase2.java:175) > > > > > at > android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164) > > > > > at > android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151) > > > > > at android.test.InstrumentationTestRunner.onStart > > > > > (InstrumentationTestRunner.java:418) > > > > > at android.app.Instrumentation$InstrumentationThread.run > > > > > (Instrumentation.java:1520)" > > > > > > > How do I fix it? > > > > > -- > > > Dianne Hackborn > > > Android framework engineer > > > [email protected] > > > > > Note: please don't send private questions to me, as I don't have time > to > > > provide private support, and so won't reply to such e-mails. All such > > > questions should be posted on public forums, where I and others can see > and > > > answer them.- Hide quoted text - > > > > > - Show quoted text -- Hide quoted text - > > > > - Show quoted text - > > > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

