ViewPager.setCurrentItem responsing NullPointerExc

2019-08-11 04:55发布

问题:

I have implemented ViewPager and fragment(say A, B, C, D, E) as an child.

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter); // FragmentStatePagerAdapter

after then I called mViewPager.setCurrentItem(3, false); which is giving NullPointerException. Following log:

java.lang.NullPointerException E/AndroidRuntime( 330): at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116) E/AndroidRuntime( 330): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832) E/AndroidRuntime( 330): at android.support.v4.view.ViewPager.populate(ViewPager.java:1048) E/AndroidRuntime( 330): at android.support.v4.view.ViewPager.populate(ViewPager.java:914) E/AndroidRuntime( 330): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) E/AndroidRuntime( 330): at android.view.View.measure(View.java:17478) E/AndroidRuntime( 330): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364) E/AndroidRuntime( 330): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) E/AndroidRuntime( 330): at android.view.View.measure(View.java:17478) E/AndroidRuntime( 330): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364) E/AndroidRuntime( 330): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410) E/AndroidRuntime( 330): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) E/AndroidRuntime( 330): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)

What I am guessing here is, might be PageViewer did not load page I am asking for, but I wait here also before to load, but it's same result.

Does any one have any suggestion for same.

Edit: Adapter class

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        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 DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.


            switch (id) {
            case 0:

                fragment = new TestFragment1();
                Bundle args = new Bundle();
                args.putInt("position", position + 1);
                fragment.setArguments(args);

                break;

            case 1:

                fragment = new TestFragment2();
                args = new Bundle();
                args.putInt("position", position + 1);
                fragment.setArguments(args);

                break;

            case 3:

                fragment = new TestFragment4();
                args = new Bundle();
                args.putInt("position", position + 1);
                fragment.setArguments(args);

                break;


            case 4:

                fragment = new TestFragment5();
                args = new Bundle();
                args.putInt("position", position + 1);
                fragment.setArguments(args);

                break;



            }

            return fragment;
        }

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

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

and code to init pageviewer and set adapter:

mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setOffscreenPageLimit(1);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);

and code to set request page:

mViewPager.setCurrentItem(viewPosition, false);