我需要在活动中放置类似浏览器的模块。我正在使用fragmentTabHost。添加按钮将在fragmentTabHost中添加选项卡,并且有关闭按钮以关闭fragmentTabHost中的选项卡。 。但它可以正常工作3到4次,但之后它会抛出NullPointerException
我认为关闭标签后会出现此异常,即添加新标签时没有错误。但是,如果我尝试选择上一个或后一个最后一个选项卡,则关闭选项卡后会发生异常。
这是我的活动代码
public class MutiTabActivity extends FragmentActivity {
TabHost tabHost;
private FragmentTabHost fragmentTabHost;
private Button addButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton = (Button) findViewById(R.id.add_new_tab);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addNewTab();
}
});
fragmentTabHost = (FragmentTabHost) findViewById(R.id.main_tab_host);
fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.frame_layout);
fragmentTabHost.getTabWidget().setDividerDrawable(android.R.color.white);
fragmentTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
onTabSelected();
}
});
addNewTab();
addNewTab();
addNewTab();
addNewTab();
}
OnClickListener closeTabListener = new OnClickListener() {
@Override
public void onClick(View v) {
TabWidget tabWidget = fragmentTabHost.getTabWidget();
tabWidget.removeViewAt(currentTabIndex);
}
};
private int currentTabIndex;
/*
* used to change the background color of selected tab and show close button
* on selected tab
*/
private void onTabSelected() {
TabWidget tabWidget = fragmentTabHost.getTabWidget();
int tabCount = tabWidget.getTabCount();
for (int i = 0; i < tabCount; i++) {
View childAt = tabWidget.getChildAt(i);
View findViewById = childAt.findViewById(R.id.close_tab_textView1);
if (findViewById != null) {
findViewById.setVisibility(View.GONE);
childAt.setBackgroundColor(Color.LTGRAY);
findViewById.setOnClickListener(null);
String currentTabTag = fragmentTabHost.getCurrentTabTag();
String tag2 = (String) childAt.getTag();
if (tag2 != null) {
if (currentTabTag.equals(tag2)) {
findViewById.setVisibility(View.VISIBLE);
findViewById.setOnClickListener(closeTabListener);
childAt.setBackgroundColor(Color.DKGRAY);
currentTabIndex = i;
}
}
}
}
}
private int lastTabCount;
private TabSpec getNewTab(String tag, String title) {
TabSpec newTabSpec = fragmentTabHost.newTabSpec(tag);
TabWidget tabWidget = fragmentTabHost.getTabWidget();
View view2 = LayoutInflater.from(this).inflate(R.layout.tab_view, tabWidget, false);
view2.setTag(tag);
TextView name = (TextView) view2.findViewById(R.id.textView1);
TextView closeBtn = (TextView) view2.findViewById(R.id.close_tab_textView1);
closeBtn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
name.setText(title);
newTabSpec.setIndicator(view2);
return newTabSpec;
}
/* add new tab in fragmentTabHost */
private void addNewTab() {
lastTabCount++;
fragmentTabHost.addTab(getNewTab("Tag " + lastTabCount, "Tab " + lastTabCount), WebViewTab.class, null);
}
}
&#13;
以下是日志
05-11 03:48:27.652: E/AndroidRuntime(11073): FATAL EXCEPTION: main
05-11 03:48:27.652: E/AndroidRuntime(11073): java.lang.NullPointerException
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.widget.TabWidget.focusCurrentTab(TabWidget.java:461)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.widget.TabHost.setCurrentTab(TabHost.java:410)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.view.View.performClick(View.java:4240)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.view.View$PerformClick.run(View.java:17721)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.os.Handler.handleCallback(Handler.java:730)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.os.Looper.loop(Looper.java:137)
05-11 03:48:27.652: E/AndroidRuntime(11073): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-11 03:48:27.652: E/AndroidRuntime(11073): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 03:48:27.652: E/AndroidRuntime(11073): at java.lang.reflect.Method.invoke(Method.java:525)
05-11 03:48:27.652: E/AndroidRuntime(11073): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-11 03:48:27.652: E/AndroidRuntime(11073): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-11 03:48:27.652: E/AndroidRuntime(11073): at dalvik.system.NativeStart.main(Native Method)
我发现了here这样的问题,但解决方案提供的对我来说并不适用。
先谢谢,请帮帮我。