提问者:小点点

如何减少TabLayout中的制表符数(每次都面临错误)


大家好,我有一个选项卡布局,有4个选项卡:

 private final String[] TITLE = {"NEAR BY", "GLOBAL", "ROOM", "PRIVATE"};

我不希望TabLayout中的“Global”选项卡,因此我将其从Title数组中移除,因此现在它看起来是这样的:

  private final String[] TITLE = {"NEAR BY", "ROOM", "PRIVATE"};

但是,每次我再次运行该应用程序时,都会出现以下错误:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

这是我的activity代码:

  public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        Log.i(TAG, "Create fragment position: " + position);
        switch (position) {
            case 0:
                return new NearbyFriendsFragment();
            case 1:
                GlobalFragment globalFragment = new GlobalFragment();
                setListener(globalFragment);
                return globalFragment;
            case 2:
                return new RoomFragment();
            default:
                return new PrivateFragment();
        }
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Log.i(TAG, "Get page title: " + position);
        return TITLE[position];
    }
}
  @Override
public void onTabUpdate(int position, int countBadge) {
    Log.i(TAG, "On Tab update: " + position + " , Count Badge: " + countBadge);
    arrayCountBadge[position] = countBadge;
    customTabView(position, countBadge);
}

我是安卓开发的初学者,请提供您的帮助来解决这个问题。 谢谢


共2个答案

匿名用户

getCount函数返回4,而数组的大小仅为3,请尝试:

@Override
public int getCount() {
    return 3;
}

匿名用户

这是完整的代码

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    int noOfItems;

    public SectionsPagerAdapter(FragmentManager FM,int itemCount) {
        super(fm);
        this.noOfItems=itemCount;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        Log.i(TAG, "Create fragment position: " + position);
        switch (position) {
            case 0:
                return new NearbyFriendsFragment();
            case 1:
                 return new RoomFragment();
            case 2:
                GlobalFragment globalFragment = new GlobalFragment();
                setListener(globalFragment);
                return globalFragment;
            default:
                return new PrivateFragment();
        }
    }

    @Override
    public int getCount() {
        return noOfItems;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Log.i(TAG, "Get page title: " + position);
        return TITLE[position];
    }
}
  @Override
public void onTabUpdate(int position, int countBadge) {
    Log.i(TAG, "On Tab update: " + position + " , Count Badge: " + countBadge);
    arrayCountBadge[position] = countBadge;
    customTabView(position, countBadge);
}

这样,大小(或项目数量)将自动更新。 希望这能帮上忙。 请随意要求澄清。。。