0

So I have an activity with a ViewPager in it. I want it to destroy all the ViewPager's Fragments on button press in the activity and update the ViewPager's Fragments with new text/Views.

I've written the code in the activity as below.

 SectionsPagerAdapter mSectionsPagerAdapter;

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), null);

    ViewPager mViewPager = (ViewPager)findViewById(R.id.vp_main);
      final PagerTabStrip strip = (PagerTabStrip)findViewById(R.id.pts_main);         
        mViewPager.setAdapter(mSectionsPagerAdapter);

        ArrayList<PagerItem> pagerItems = new ArrayList<PagerItem>();
        pagerItems.add(new PagerItem("Fragment1", new Fragment1())); //Error here when...
        pagerItems.add(new PagerItem("Fragment2", new Fragment2()));
        pagerItems.add(new PagerItem("Fragment1", new Fragment3()));
        pagerItems.add(new PagerItem("Fragment2", new Fragment4()));
        pagerItems.add(new PagerItem("Fragment1", new Fragment5()));
        pagerItems.add(new PagerItem("Fragment2", new Fragment6()));


      mSectionsPagerAdapter.setPagerItems(pagerItems); // I changed to my custom adapter here
      mSectionsPagerAdapter.notifyDataSetChanged();

ERROR MESSAGE IS... No enclosing instance of type SectionsPagerAdapter is accessible. Must qualify the allocation with an enclosing instance of type SectionsPagerAdapter (e.g. x.new A() where x is an instance of SectionsPagerAdapter).

Can someone shed light on this issue?? Much appreciated

1 Answers1

0

Make sure the class SectionsPagerAdapter exists and its PUBLIC and its constructor is PUBLIC.

Like this...

public class SectionsPagerAdapter {
      public SectionsPagerAdapter() {
      }
}

And replace this

new SectionsPagerAdapter(getSupportFragmentManager(), null);

with this

this.new SectionsPagerAdapter(getSupportFragmentManager(), null);

Or you can set SectionsPagerAdapter as STATIC. e.g.

public static class SectionsPagerAdapter {
      public SectionsPagerAdapter() {
      }
}

Good Luck. :)

Hemendra Sharma
  • 1,063
  • 9
  • 21
  • Yes The class SectionsPagerAdapter does exist as you illustrate and is set to PUBLIC, only it is in my SectionPagerAdapter.java class. Is this correct? –  Dec 19 '14 at 17:43