hoshiKuzu
hoshiKuzu

Reputation: 915

NullPointerException with ViewPager after the app has been killed

I have 3 pages in a ViewPager: instances of Tab0, Tab1, Tab2. In the main Activity, I'm storing the instance of each tab in separte variables from the SectionsPagerAdapter's getItem() function.

I assume that ViewPager always initializes the current tab, the tab to the left of it, and the tab to the right of it.

These are my instances: Tab0 t0; Tab1 t1; Tab2 t2;

The app works fine initially. But if i press the home button (to trigger onPause()), kill the app from task killer (or assume that android itself has killed it), and open the app again, i get NullPointerExceptions in onPageSelected because t0, t1 and t2 are now null.

This is my onCreate method:

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

            mSectionsPagerAdapter = new SectionsPagerAdapter(
                    getSupportFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);
            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageSelected(int pos) {

                    switch(pos){
                    case 1:
                       t1.lookup()
                        break;
                    case 0:

                    t0.doSomething();

                        break;
                    case 2:
                            t2.someThingElse();
                        break;

                    }

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {


                }
            });


    mViewPager.setCurrentItem(1,true);
}

This is my FragmentPagerAdapter (which is a nested class inside my 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 DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment;

        switch(position){
        case 0:
            fragment = t0 = new Tab0();
            break;
        case 1:
            fragment = t1 = new Tab1();         
            break;
        case 2:
            fragment = t2 = new Tab2();
            break;

            default:
                fragment=null;
        }


        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, position + 1);
        if(fragment!=null)
            fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 1:
            return getString(R.string.T1).toUpperCase(l);
        case 0:
            return getString(R.string.T0).toUpperCase(l);
        case 2:
            return getString(R.string.T2).toUpperCase(l);
        }
        return null;
    }
}

Upvotes: 2

Views: 527

Answers (1)

Jim Baca
Jim Baca

Reputation: 6132

From the Docs: The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. Meaning when it comes back the fragments will be recreated for you but will not be the original ones(hence your loss of t* variables). I'd take a look at overriding saveState and restoreState methods to see if you can add the logic for setting up what the t* variables. If not perhaps in one of the other on* methods in the fragment.

Upvotes: 1

Related Questions