Android - Tabhost working in Activity class

前端 未结 7 937
温柔的废话
温柔的废话 2020-12-09 12:28

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

相关标签:
7条回答
  • 2020-12-09 12:28

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

    0 讨论(0)
  • 2020-12-09 12:29

    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);
    
    0 讨论(0)
  • 2020-12-09 12:34

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

    0 讨论(0)
  • 2020-12-09 12:35

    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);
    
    0 讨论(0)
  • 2020-12-09 12:38

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

    0 讨论(0)
  • 2020-12-09 12:46

    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

    0 讨论(0)
提交回复
热议问题