今天使用TabHost控件,竟然半天没弄出来,一直报NULL;
原来是少了
tabHost.setup();
API说明如下:
void android.widget.TabHost.setup()
public void setup ()
Added in API level 1
Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in TabActivity. Example:
mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
使用TabActivity getTabHost()方法,就不用调用 setup() 方法。
而使用findViewById 则需要调用。
host=(TabHost) this.findViewById(android.R.id.tabhost);//获得 android:id="@android:id/tabhost" 控件id
host.setup();
host.addTab(host.newTabSpec("TAB1").setContent(R.id.tab1).setIndicator("TAB1"));
host.addTab(host.newTabSpec("TAB2").setContent(R.id.tab2).setIndicator("TAB1"));
host.addTab(host.newTabSpec("TAB3").setContent(R.id.tab3).setIndicator("TAB1"));