Android - Tabhost working in Activity class

烈酒焚心 提交于 2019-11-28 08:34:21

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).

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);
Saad Farooq

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);

android:id="@+id/tabhost" doesnt work for me. I have to write android:id="@android:id/tabhost"

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

Change the class declaration which has this:

public class my_proto extends Activity

to

public class my_proto extends TabActivity

What tommie says is true. You should also look at the tutorial here http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!