Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在tabhost android中更新特定选项卡图标?_Android_Android Tabhost - Fatal编程技术网

如何在tabhost android中更新特定选项卡图标?

如何在tabhost android中更新特定选项卡图标?,android,android-tabhost,Android,Android Tabhost,我有4个标签,其中一个是通知图标。默认情况下,使用选项卡规格设置所有图标 spec = host.newTabSpec("Tab Four"); spec.setContent(R.id.tab5); spec.setContent(new Intent(getApplicationContext(), ICNotificationActivity.class));

我有4个标签,其中一个是通知图标。默认情况下,使用选项卡规格设置所有图标

spec = host.newTabSpec("Tab Four");
                        spec.setContent(R.id.tab5);
                        spec.setContent(new Intent(getApplicationContext(), ICNotificationActivity.class));
                            spec.setIndicator("", getResources().getDrawable(R.drawable.tab_notification_noted));
host.addTab(spec);

延迟一段时间后,我必须用另一个资源文件更新选项卡4图标。如何更新tabhost?是否有可用的更新功能,如主机.addTab(spec)?

这里我将循环所有选项卡并更改所选选项卡的颜色和图标。 在循环中,我首先将“所有”选项卡设置为“未选定”,而不是将“仅选定”选项卡设置为“更新的图标和颜色”

private void setTabColor(TabHost tabhost, int position) {
    for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
        //      unselected
        tabhost.getTabWidget().getChildAt(i)
                .setBackgroundResource(R.drawable.tab_unselected);

        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tvTabTitle);
        tv.setTextColor(getResources().getColor(R.color.text_white));
    }
    if (position > 0) {
        //      selected
        tabhost.getTabWidget().setCurrentTab(position);
        tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
                .setBackgroundResource(R.drawable.tab_selected);
        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(tabhost.getCurrentTab()).findViewById(R.id.tvTabTitle);
        tv.setTextColor(getResources().getColor(R.color.tab_color));
    }

}

java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.view.view.setBackgroundResource(int)”。显示此日志错误。{host=(TabHost)findViewById(R.id.TabHost);host.setup();host.getTabWidget().setTripEnabled(false);host.getTabWidget().setDividerDrawable(R.drawable.border_tab_activity);host.setup(this.getLocalActivityManager());},已经定义了tabhost。@SARATHV我已经给出了我的代码,您需要根据需要进行更改。代码工作正常,谢谢。我在tabposition中犯了错误,这就是为什么我得到了null指针异常
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
    @Override
    public void run() {
        //Do something here
     setTabColor(myTabHost, myTabPosition);
    }
}, 5000);