OK, I must be overlooking something silly because I am unable to get my tabhost to display in my Activity class. I am getting the dreaded force close when I try to run the
android:id="@+id/tabhost" doesnt work for me. I have to write android:id="@android:id/tabhost"
If you have a scenario in which you need to use Activity class for your Tabhost you can try the following it works.
tabHost = (TabHost) findViewById(R.id.tabhost); //here tabHost will be your Tabhost
LocalActivityManager mLocalActivityManager = new LocalActivityManager(mActivity, false);
mLocalActivityManager.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
tabHost.setup(mLocalActivityManager);
Ok I figured it out. Apparently, TabActivity extends ActivityGroup, which extends Activity. But in your code your class extends Activity which is not an activity group.
So there are two options:
1) If you want the tab content to be activities, have your class extend ActivityGroup (instead of Activity). Then your call to setup should be host.setup(getLocalActivityManager());
This way you are emulating the TabActivity source code.
2) If you can have your tab content be views (vs activities), keep your class as extending from Activity, and keep your call to setup(). But for the setContent part do something like this:
host.addTab(host.newTabSpec("two")
.setIndicator("Second Results")
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return new TextView(TestActivity.this);
}
}));
And then define your list view inside createTabContent (that's usually what I do - I prefer using views instead of activities as the contents of the tabs).
Had the same problem: this question helps Android: TabHost without TabActivity
Apparently, you only need to add one line:
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
host.setup(mLocalActivityManager);
What tommie says is true. You should also look at the tutorial here http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
I changed the class to
public class my_proto extends ActivityGroup
and change the following:
this.mHost = (TabHost) findViewById(R.id.tabhost);
this.mHost.setup(getLocalActivityManager());
It seems ok for me