Android:没有TabActivity的TabHost

我想创build标签, 而不扩展TabActivity。 (原因是TabActivity无法处理自定义标题栏,因为它似乎)。 我有

public class startTab extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); Resources res = getResources(); LocalActivityManager mlam = new LocalActivityManager(this, false); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); tabHost.setup(mlam); TabHost.TabSpec spec; Intent intent; intent = new Intent().setClass(this, Show1.class); spec = tabHost.newTabSpec("Items").setIndicator("Items", res.getDrawable(R.drawable.items32_ldpi)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, Show2.class); spec = tabHost.newTabSpec("Users").setIndicator("Users",res.getDrawable(R.drawable.user32_ldpi)).setContent(intent); tabHost.addTab(spec); } 

}

我得到的错误是

  07-02 07:11:12.715: ERROR/AndroidRuntime(411): Caused by: java.lang.IllegalStateException: Activities can't be added until the containing group has been created. 

该视图的XML是

 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="5dip"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="fill_parent"></TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="5dip"> </FrameLayout> </LinearLayout> </TabHost> 

我读的地方,我不得不使用一个LocalActivityManager,我认为我失去了一些东西。 任何人的想法?

谢谢!

在调用tabHost.setup(mLocalActivityManager)之前; 你需要添加这一行。

 mlam.dispatchCreate(savedInstanceState); tabHost.setup(mlam ); 

同样,你需要为onResume添加,

 mlam.dispatchResume(); 

在onPause()

  mlam.dispatchPause(isFinishing()); 

请考虑使用Views作为您的标签的内容。 这不仅会导致更less的代码,更less的堆空间消耗,更less的堆栈空间消耗和更低的CPU利用率,还可以解决这个问题。 这里有两个 例子显示这种技术。

 public class ScoreboardActivity extends Activity { LocalActivityManager mlam; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scoreboard); mlam = new LocalActivityManager(this, false); mlam.dispatchCreate(savedInstanceState); TabHost th = (TabHost) findViewById(android.R.id.tabhost); th.setup(mlam); th.addTab(th.newTabSpec("Numpad").setIndicator("Numpad").setContent(R.id.tab1)); th.addTab(th.newTabSpec("CardCount").setIndicator("CardCount").setContent(R.id.tab2)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_scoreboard, menu); return true; } @Override protected void onResume(){ super.onResume(); mlam.dispatchResume(); } @Override protected void onPause(){ super.onPause(); mlam.dispatchPause(isFinishing()); } } 

尽pipe有devise上的考虑,但下面的方法根本不起作用,API似乎表明setContent(Intent i)是有效的。 当这个活动扩展了TabActivity时候,这是有效的,然而,扩展Activityexception at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)添加setup()调用结果的exception at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)

有趣的是,LogCatbuild议我忘记调用setup()

 mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); Intent tab1Intent = new Intent(this, ActivityOne.class); Button tab1View = new Button(this); tab1View.setText("Activity 1"); mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator(tab1View).setContent(tab1Intent));