1

Edited for a little clarity So ... I must be misunderstanding how to properly use TabHosts, because my code crashes completely during setup when I try to add some TabSpecs.. I would like to have a view that has some UI interface with graphics associated with it and animations, and then a menu system that uses a tab setup. My code looks something along the lines of:

public void myView extends RelativeLayout {

    // Other Views ...
    private TabHost myTabHost;
    private ExpandableListView listView1; // content for tab 1
    private ExpandableListView listView2; // content for tab 2

    public myView(Context context) {
        super(context);

        // some other stuff
        myTabHost = new TabHost(context);
        myTabHost.setId(R.id.myTabHost);

        listView1 = new ExpandableListView(context);
        listView1.setId(R.id.myExpandableListView1);

        listView2 = new ExpandableListView(context);
        listView2.setId(R.id.myExpandableListView2);

        TabSpec tab1 = myTabHost.getTabSpec(res.getString(R.string.tab1));
        tab1.setIndicator(res.getString(R.string.tab1), res.getDrawable(R.drawable.tab1));
        tab1.setContent(R.id.myExpandableListView1); // *********
        myTabHost.addTab(tab1);

        // do something similar for tab2..
    }

}

Here's the Activity I'm currently using..

public void myActivity extends Activity {

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        // do a little stuff..

        TabHost myTabHost = (TabHost) findViewById(R.id.myTabHost); // not sure if I'm supposed to set up the tabs in my activity, or not..
        // I tried it after it crashing in the view, and it still crashed in the activity..
        ExpandableListView myListView1 = (ExpandableListView) findViewById(R.id.myExpandableListView1);
        // set up expandable list view the way I want from data sources..

        // do something similar for myListView2
    }

}

From what I understand, I don't want to extend TabActivity because that assumes the whole screen is going to be one giant TabHost, right? I only want the TabHost to be a small portion of the Activity... The problem is that the system crashes where I have indicated by the *********'s.. I guess I just don't understand properly how to go about setting up the tabs? Could someone please enlighten me as to the proper way to do it, or maybe suggest why it's crashing? I guess I should also add the question .. to use a TabHost, do I HAVE to use a TabActivity? (I don't see why I would have to, but Android can be funny that way..)

BONUS I was poking around and found this stackoverflow link regarding tabhosts without tabactivities.. They reference a LocalActivityManager. I'll have to read about that..

Community
  • 1
  • 1
Joishi Bodio
  • 438
  • 6
  • 17

2 Answers2

0

I'm pretty sure that you need an Activity to use the TabHost like you want to. Also, TabHost requires a very specific layout to be present for it to work, which you don't have either. I believe you need to rethink your approach.

I'm pretty sure you've seen this, but just in case.

Edit

You can extend from Activity (instead of TabActivity) and place your tab widgets wherever you want. However, you MUST call tabHostVariable.setup() before you add any TabSpecs.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • I guess I should add in my activity... I'll edit my post to fill it out more. – Joishi Bodio Jun 09 '11 at 04:32
  • I did try running setup() on myTabHost before posting here, but it just caused the app to crash. Is my problem that I don't have a TabWidget? The docs say that a TabHost needs a TabWidget and a FrameLayout - but that's also in relation to a TabActivity.. And I wasn't sure if the TabWidget and FrameLayout were internal to the TabHost class, anyway. (are they 3 separate pieces that need to be linked together?) I've looked at the hello tabhost several times, but it doesn't really seem to address the issue I've been having.. And reading the docs for TabHost hasn't been explaining it either. – Joishi Bodio Jun 09 '11 at 20:13
  • Yep, you need all three. A tabhost at the top (doesn't render anything), a tabwidget (which will hold your actual tabs) and a tabcontent somewhere, wherever (as long as they're inside the tabhost!). – dmon Jun 09 '11 at 20:14
  • I just ran setup() again and looked at the exception, and the message is "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'". So I guess that's my first problem is that I don't have a tabwidget. So then I guess I need to know how to go about creating/setting up a tabwidget/tabcontent correctly? Do I have to run myTabHost.getTabWidget() and then modify that object for the widget? What about the FrameLayout? (as you can see in my code, I'd like the content to be ExpandableListViews..) I appreciate your help, btw. :) – Joishi Bodio Jun 09 '11 at 21:37
  • ok n/m.. I'm getting it now. I created a TabWidget(context) object and set it's ID to android.R.id.tabs and created a FrameLayout(context) object and set it's ID to android.R.id.tabcontent and then did TabHost.addView(TabWidget) and TabHost.addView(FrameLayout) ... I'm on my way to getting it working correctly. (still giving me errors, but now it's about different stuff, so I should be ok). Thanks! – Joishi Bodio Jun 09 '11 at 22:10
0

You want to extend TabActivity to do this properly. Check out http://developer.android.com/resources/tutorials/views/hello-tabwidget.html for a great tutorial.

zienkikk
  • 2,404
  • 1
  • 21
  • 28