TabHost error when adding TabSpecs
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..
ExpandableListV开发者_运维百科iew 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..
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
.
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.
精彩评论