我正在尝试开发一个代码,就像我喜欢在Add按钮上一样,它会添加Tab而且还有Del按钮来删除标签..但是它工作正常3到4次但是之后它会抛出错误...
和Log-Cat看起来像这样: -
02-28 16:33:37.832: E/AndroidRuntime(7830): FATAL EXCEPTION: main
02-28 16:33:37.832: E/AndroidRuntime(7830): java.lang.NullPointerException
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.widget.TabWidget.focusCurrentTab(TabWidget.java:461)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.widget.TabHost.setCurrentTab(TabHost.java:410)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.view.View.performClick(View.java:4223)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.view.View$PerformClick.run(View.java:17487)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.os.Handler.handleCallback(Handler.java:800)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.os.Handler.dispatchMessage(Handler.java:100)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.os.Looper.loop(Looper.java:194)
02-28 16:33:37.832: E/AndroidRuntime(7830): at android.app.ActivityThread.main(ActivityThread.java:5371)
02-28 16:33:37.832: E/AndroidRuntime(7830): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 16:33:37.832: E/AndroidRuntime(7830): at java.lang.reflect.Method.invoke(Method.java:525)
02-28 16:33:37.832: E/AndroidRuntime(7830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-28 16:33:37.832: E/AndroidRuntime(7830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-28 16:33:37.832: E/AndroidRuntime(7830): at dalvik.system.NativeStart.main(Native Method)
和
MainActivity.java
package com.example.sampletab;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
private static int tabIndex = 0;
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// set the First Tab as selected Tab.
setSelectedTabColor();
// setTabColor(tabHost);
}
});
// setTabColor(tabHost);
addTabs();
delTabs();
((Button) findViewById(R.id.add_tab))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tabIndex++;
addTabs();
}
});
((Button) findViewById(R.id.del_tab))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
delTabs();
}
});
}
protected void setSelectedTabColor() {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.LTGRAY);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundColor(Color.RED);
}
private void delTabs() {
tabHost.getTabWidget().removeView(
tabHost.getTabWidget().getChildTabViewAt(
tabHost.getCurrentTab()));
}
private void addTabs() {
// TODO Auto-generated method stub
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
Button tabBtn = (Button) layoutInflater.inflate(R.layout.tab_event,
null);
tabBtn.setText("Tabs" + tabIndex);
Intent tabintent = new Intent(MainActivity.this, BlankActivity.class);
setupTab(tabBtn, tabintent, "Tab" + tabIndex);
}
private void setupTab(View tabBtn, Intent setIntent, String tag) {
// TODO Auto-generated method stub
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn)
.setContent(setIntent);
tabHost.addTab(setContent);
}
}
答案 0 :(得分:1)
在向TabHost添加标签之前,您需要设置可绘制的分隔符。
以下一行:
tabHost.getTabWidget().setDividerDrawable(R.color.light);
应该向上移动。您可以直接致电:
TabHost tabHost = getTabHost();