我的代码是这样的:
公共无效 removeTabForLogout(){
if (tabHost.getTabWidget().getChildCount()==4){
tabHost.setCurrentTab(0);
tabHost.clearAllTabs();
tabHost.addTab(getTab1());
tabHost.addTab(getTab2());
}
}
私有 TabSpec getTab1(){
if (mTab1==null){
mTab1= tabHost.newTabSpec("tab1")
.setIndicator(...).setContent(intentForTab1);
}
return mTab1;
}
私有 TabSpec getTab2(){
if (mTab1==null){
mTab1= tabHost.newTabSpec("tab2")
.setIndicator(...).setContent(intentForTab2);
}
return mTab2;
}
我在 TabHost 中有 4 个选项卡:tab1、tab2、tab3、tab4,当用户注销时,我将删除 tab3 和 tab4,所以我调用 removeTabForLogout(),清除 tabHost 中的所有选项卡并重新添加 tab1 和 tab2。
在 android 1.5 和 android 1.6 中,如果当前选择的选项卡是 tab1,当我调用 removeTabForLogout 时,tabHost.addTab(getTab1()) 会抛出异常:
ERROR/AndroidRuntime(205): java.lang.IllegalStateException: View com.android.internal.policy.impl.PhoneWindow$DecorView@437aba18 已经添加到窗口管理器中。
在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:125)
...........
在 android.widget.TabHost.setCurrentTab(TabHost.java:320)
在 android.widget.TabHost.addTab(TabHost.java:210)
在 aitao.mobile.android.actMain.removeTabForLogout(actMain.java:266)
但 removeTabForLogout() 在 android 2.0 及更高版本中工作正常。
我在 Google 上找不到任何有用的信息。有没有人可以帮助我?非常感谢!