如何修复以编程方式设置选项卡背景颜色时出现的此错误?

发布于 2024-12-08 14:03:54 字数 2105 浏览 1 评论 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.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

束缚m 2024-12-15 14:03:54

我遇到了这个问题,解决方法很好也很简单:

在将选项卡添加到 TabHost 之前,您需要设置可绘制的分隔线。

因此以下行:

tabHost.getTabWidget().setDividerDrawable(R.color.light);

应该向上移动。您可以直接调用 if:

TabHost tabHost = getTabHost();

对我来说,这解决了问题。希望它能帮助别人!

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:

tabHost.getTabWidget().setDividerDrawable(R.color.light);

should be moved up. You can call if directly after:

TabHost tabHost = getTabHost();

For me, this solved the problem. Hope it helps someone!

时间海 2024-12-15 14:03:54

在设置颜色之前对子项进行空检查..

Do null check for child before setting the color ..

长伴 2024-12-15 14:03:54

尝试使用这个:

 //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
}

Try using this:

 //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);

here is the setTabColor() method:

//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
}
嘿看小鸭子会跑 2024-12-15 14:03:54

请!从您的代码中删除以下行:

tabHost.getTabWidget().setDividerDrawable(R.color.light);

please! remove below line from your code:

tabHost.getTabWidget().setDividerDrawable(R.color.light);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文