Android:创建后打开选项卡

Android:创建后打开选项卡,android,android-fragments,android-tabs,Android,Android Fragments,Android Tabs,在活动中,我希望在某些事件中打开和关闭选项卡。tabHost是使用 FragmentTabHost tabHost= (FragmentTabHost) findViewById(R.id.fragment_tab_host); tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 我使用以下行创建选项卡: TabSpec tabSpec= tabHost.newTabSpec(tabName);

在活动中,我希望在某些事件中打开和关闭选项卡。tabHost是使用

FragmentTabHost tabHost= (FragmentTabHost) findViewById(R.id.fragment_tab_host);
tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
我使用以下行创建选项卡:

TabSpec tabSpec= tabHost.newTabSpec(tabName);
tabSpec.setIndicator(dispName);
tabHost.addTab(tabSpec, MyActivityFragment, new Bundle());

我现在如何打开选项卡?我尝试使用
tabHost.setCurrentTabByTag(tabName)。但是,这有时会引发NullPointerException

可能重复的,请用所有代码编辑。