若是用系統默認的tabhost時,即(TabHost)findViewById(android.R.id.tabhost); 紅色部分爲android系統自帶的樣式java
直接用getTabhost()初始化,整個類繼承tabActivity。android
若是使用本身定義的id時,必須使用findViewById(R.id.tabhostname);app
setup()或者setup(LocalActivityManager activityGroup)ide
若是後面使用 setContent(android.content.Intent)即setContent參數是一個實例化的Intent時this
必須使用setup(LocalActivityManager activityGroup),而對應的整個類必須繼承ActivityGroupspa
使用setup()時,整個類繼承的是Activity。unix
在使用TabHost時有時出現Did you forget to call 'public void setup(LocalActivityManager activityGroup),主要是沒有繼承ActivityGroup和得到ActivityManager。orm
E/AndroidRuntime(20559): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
E/AndroidRuntime(20559): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
E/AndroidRuntime(20559): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
E/AndroidRuntime(20559): at android.widget.TabHost.addTab(TabHost.java:216)
E/AndroidRuntime(20559): at org.ourunix.android.tabdesign.TabDesignActivity.onCreate(TabDesignActivity.java:47)
E/AndroidRuntime(20559): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
E/AndroidRuntime(20559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)繼承
你的Activity繼承ActivityGroup,並得到ActivityManager。代碼以下:get
- public class TabDesignActivity extends ActivityGroup{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tab_main);
- mTabHost = (TabHost) findViewById(R.id.tabhost);
- mTabHost.setup();
- mTabHost.setup(this.getLocalActivityManager());
- }