拆除其内部的标签和活动(意向)从TabHost [英] Removing a tab and the activity (intent) inside of it from a TabHost
问题描述
我有一个应用程序可以动态地创建标签。当我创建一个标签我发起一个活动的意图。像这样:
I have an app that can create tabs dynamically. And when I create a tab I initiate an activity as an intent. Like so:
private void addTab(Context packageContext, Class<?> newClass, TabHost mTabHost, String tabId, String tabLabel){
// newClass is my Activity class that I want to start in the tab
Intent intent = new Intent().setClass(packageContext, newClass);
TabHost.TabSpec spec;
spec = mTabHost.newTabSpec(tabId).setIndicator(tabLabel)
.setContent(intent);
mTabHost.addTab(spec);
mTabHost.setCurrentTabByTag(tabId);
}
pretty的标准。而且它的伟大工程。现在,假设我有一个按钮(或者菜单项,等等)在我实例化我的标签内的活动。当用户presses此按钮,我想要的活性,和接头是内侧,被除去并销毁。
Pretty standard. And it works great. Now, suppose that I have a button (or menuitem, whatever) in the activity that I instantiated inside of my tab. When the user presses this button, I want the activity, and the tab it is inside of, to be removed and destroyed.
我似乎无法找到一个简单的方法来做到这一点。我已经找到了TabHost.clearAllTabs()函数,但是这会破坏所有选项卡和活动,我只是想删除一个。
I can't seem to find a simple way to do this. I have found the TabHost.clearAllTabs() function, but this destroys all tabs and activities, I just want to remove one.
有人建议我救,我已经打开的所有标签的列表,然后调用clearAllTabs(),之后我重新我所有的其他选项卡,除了我不想要的。
Someone suggested I save a list of all Tabs that I have opened, and then call clearAllTabs(), after which I recreate all of my other tabs except for the one I don't want.
事情是这样的:
public static ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
我加入这一行我addTab()函数,使每片创建是记得在我的ArrayList
I add this line to my addTab() function so that every tab I create is remember in my ArrayList:
list.add(spec);
然后当我想删除我的标签我运行这个功能:
And then when I want to remove my tab I run this function:
public static void removeTab(){
list.remove(list.size()-1); // remove it from memory
mTabHost.clearAllTabs(); // clear all tabs from the tabhost
for(TabHost.TabSpec spec : list) // add all that you remember back
mTabHost.addTab(spec);
}
这将删除我的选项卡从我的ArrayList中,去除所有标签,然后重新创建所有使用我的ArrayList剩余的选项卡。从理论上讲它应该工作,但我得到以下错误,当我尝试调用这个函数:
This removes my tab from my ArrayList, removes all tabs, then recreates all the tabs remaining using my ArrayList. In theory it should work, but I get the following error when I try call this function:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.TabWidget.setCurrentTab(TabWidget.java:342)
at android.widget.TabWidget.focusCurrentTab(TabWidget.java:366)
at android.widget.TabHost.setCurrentTab(TabHost.java:323)
at android.widget.TabHost.addTab(TabHost.java:216)
at com.example.myapp.TabManager.removeTab(QuikBrowser.java:86)
at com.example.myapp.TabManager.TabWindow.onOptionsItemSelected(TabWindow.java:91)
at android.app.Activity.onMenuItemSelected(Activity.java:2205)
由于某些原因,添加标签时,它会尝试设置当前选项卡,然后点击它一个空指针异常。
For some reason, when adding a tab, it attempts to set the current tab, and it hits a null pointer exception.
如果你们能提出另一种方式实现了我想要做的,还是有办法来解决我目前的方法,我想AP preciate吧。
If you guys could suggest another way of achieving what I want to do, or a way to fix my current method, I would appreciate it.
推荐答案
试着改变当前选项卡为0。
Try changing current tab to 0.
是这样的:
getTabHost().setCurrentTab(0);
getTabHost().clearAllTabs();
我在读,在调用 clearAllTabs();
将抛出一个 NullPointerException异常
如果不设置tabhost到第一个选项卡( .setCurrentTab(0)
)之前调用( .clearAllTabs()
)
I was reading that calling clearAllTabs();
will throw a nullpointerexception
if you don't set the tabhost to the first tab (.setCurrentTab(0)
) before calling (.clearAllTabs()
)
另外这个答案可以帮助? (如何从TabHost 删除标签)
Also this answer may help? (How to remove tab from TabHost)
这篇关于拆除其内部的标签和活动(意向)从TabHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!