In my app, I’ve got 3 tabs. On clicking on each individual tab, some tab specific task happens. Now I am supposed to test the scenario of changing tabs from an android test project. In order to do that, I’ve called getTabHost.setCurrentTab(int tabIndex) method. But an error has been thrown saying
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
The exception is quite self explanatory: “Only the original thread that created a view hierarchy can touch its views.” In other words, you’re trying to change the current tab from a different thread than the one that created it. More specifically, you have to do this action on the widget from the GUI thread – the thread that created the view hierarchy.
So you either have to move your
setCurrentTab(int tabIndex)call to the GUI thread, or use a message mechanism. The latter is usually done using aHandler. Plenty of good examples and code snippets ‘out there’ that can show you how to go about this.