我使用此代码为我的标签设置自定义背景颜色
//setting up the tabs
res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, First.class);
spec = tabHost.newTabSpec("one").setIndicator("One")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Second.class);
spec = tabHost.newTabSpec("two").setIndicator("Two")
.setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.color.light);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.color.light);
tabHost.getTabWidget().setDividerDrawable(R.color.light);
tabHost.setCurrentTab(0);
页面加载时,一切正常,出现Tab 0。但是,当我点击第二个选项卡时,应用程序崩溃,我收到错误
10-06 10:54:39.516: ERROR/AndroidRuntime(920): FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.TabWidget.focusCurrentTab(TabWidget.java:370)
at android.widget.TabHost.setCurrentTab(TabHost.java:323)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
任何想法如何解决这个问题?
PS。如果我评论设置bg颜色的3行,则选项卡小部件可以正常工作。
答案 0 :(得分:3)
我遇到了这个问题,修复很简单:
在向TabHost添加标签之前,您需要设置可绘制的分隔符。
以下一行:
tabHost.getTabWidget().setDividerDrawable(R.color.light);
应该向上移动。您可以直接致电:
TabHost tabHost = getTabHost();
对我来说,这解决了这个问题。希望它可以帮助别人!
答案 1 :(得分:0)
在设置颜色之前,请对子项进行空检查。
答案 2 :(得分:0)
尝试使用:
//setting up the tabs
res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, First.class);
spec = tabHost.newTabSpec("one").setIndicator("One")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Second.class);
spec = tabHost.newTabSpec("two").setIndicator("Two")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
setTabColor(tabHost);
}
});
setTabColor(tabHost);
这是setTabColor()方法:
//Change The Backgournd Color of Tabs
public void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(COLOR_DARK); //unselected
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_LIGHT); // selected
}
答案 3 :(得分:0)
请!从代码中删除以下行:
tabHost.getTabWidget().setDividerDrawable(R.color.light);