Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty ViewPager crashes with PagerTabStrip

When using a ViewPager with PagerTabStrip:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_tab_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    />

</android.support.v4.view.ViewPager>

with pager having an empty adapter:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ViewPager pager = (ViewPager) findViewById(R.id.pager);

    FragmentPagerAdapter adapter = new FragmentPagerAdapter(getFragmentManager()) {

        @Override
        public CharSequence getPageTitle(int position) {
            return "test";
        }

        @Override
        public Fragment getItem(int i) {
            return null;
        }

        @Override
        public int getCount() {
            return 0;
        }
    };
    pager.setAdapter(adapter);

}

App crashes on touching/scrolling the Tab Strip:

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
        at java.util.ArrayList.get(ArrayList.java:304)
        at android.support.v4.view.ViewPager.performDrag(ViewPager.java:1990)

I think this line in ViewPager.performDrag() might be the cause.

Is it a bug or something wrong with my view setup ?

like image 908
S.D. Avatar asked Jan 21 '13 18:01

S.D.


1 Answers

In ViewPager code array items are accessed without any checks (when there is no page ):

final ItemInfo firstItem = mItems.get(0);
final ItemInfo lastItem = mItems.get(mItems.size() - 1);

This causes it to crash.

like image 171
S.D. Avatar answered Oct 04 '22 23:10

S.D.