为什么我会得到IndexOutOfBoundsException?

时间:2016-03-03 04:06:21

标签: for-loop indexoutofboundsexception

这是我的for循环,我的for循环错了吗?我很长时间没有做过循环,所以我还是初学者,如果有人能看看我的问题,我真的很感激。

for (int i = 0; i < tabLayout.getTabCount(); i++) {
    TabLayout.Tab tab = tabLayout.getTabAt(i);
    if (tab != null) {
        tab.setCustomView(mSectionsPagerAdapter.getTabView(i));
    }else{
        System.out.println("ERROR---TAB IS NULL---ERROR");
    }

}

这是我的Count方法和Char

@Override
public int getCount() {
    // Show 3 total pages.
    return 4;
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    // return tabTitles[position];

    // getDrawable(int i) is deprecated, use getDrawable(int i, Theme theme) for min SDK >=21
    // or ContextCompat.getDrawable(Context context, int id) if you want support for older versions.
    // Drawable image = context.getResources().getDrawable(iconIds[position], context.getTheme());
    // Drawable image = context.getResources().getDrawable(imageResId[position]);

    Drawable image = ContextCompat.getDrawable(MainActivity.this, imageResId[position]);
    image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
    SpannableString sb = new SpannableString(" ");
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}

0 个答案:

没有答案