如何修复以编程方式设置选项卡背景颜色时出现的此错误?
我使用此代码为我的选项卡设置自定义背景颜色
//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);
当页面加载时,一切都很好,并且出现选项卡 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)
任何想法如何解决这个问题?
附言。如果我注释设置背景颜色的 3 行,选项卡小部件将正常工作。
I use this code to set custom background color to my tabs
//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);
When the page loads, all is fine and Tab 0 appears. But when I click on the second tab, the app crashes and I get an error
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)
Any ideas how to solve this?
PS. If I comment the 3 lines that set bg colors, the tab widget works without errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了这个问题,解决方法很好也很简单:
在将选项卡添加到 TabHost 之前,您需要设置可绘制的分隔线。
因此以下行:
应该向上移动。您可以直接调用 if:
对我来说,这解决了问题。希望它能帮助别人!
I ran into this problem and the fix was nice and simple:
You need to set the divider drawable BEFORE you add tabs to the TabHost.
So the following line:
should be moved up. You can call if directly after:
For me, this solved the problem. Hope it helps someone!
在设置颜色之前对子项进行空检查..
Do null check for child before setting the color ..
尝试使用这个:
这是 setTabColor() 方法:
Try using this:
here is the setTabColor() method:
请!从您的代码中删除以下行:
please! remove below line from your code: