0

Here's an overview of my program:

Android 2.2

  • MainActivity (tabhost)

4 Tabs

  • Search (uses SearchActivity) (default)
  • Artists (uses ArtistsActivity)
  • Albums (uses AlbumsActivity)
  • Songs (uses SongsActivity)

The app loads up to the default Search tab and SearchActivity. I can type in a search string and click search (this stores data in a global variables class). Now when I manually click on the Artists tab, it loads ArtistsActivity and the appropriate view and it grabs data from this global variables class and does stuff.

I can manually click on all tabs and switch between the tabs just fine.

However, if I use any type of setCurrentTab command to automatically switch to the next tab for the user, the program crashes with a null pointer exception.

I've tried multiple different methods I found online and not a single one of them worked.

The apps works completely fine with manually clicking tabs as a user. But I need to program this ability to happen automatically.

Example for what needs to happen automatically: App Loads->SearchTab->User types in search string and clicks Search button->ArtistsTab

It doesn't make any sense. Why can't I program the ability to automatically switch to the next tab when I can manually switch to the next tab as a user by clicking on it? It already works just fine, I just need to replicate the action of clicking on these tabs within the code so the user doesn't have to manually click the next tab.

Anyone have any suggestions?

1 Answers1

0

declare your tabhost/tabwidget as static and call setcurrent tab accessing that static tabwidget from your activity ex-

TabActivity.tabhost.setcurrentTab(1);
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
  • I had been trying that and receiving null pointer exception. I did find a solution though that I'll post below. In the tab activity: `TabHost tabHost = (TabHost) getParent().findViewById(android.R.id.tabhost);` In the OnClickListener: `tabHost.setCurrentTab(1);` It appears the getParent() before the findViewByID is necessary to fix this issue. So I just declare a tabHost in each tab activity using that first line and am able to switch between tabs flawlessly. – user1284430 Mar 21 '12 at 21:48