Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swipe back from a viewpager causes the app to crash

I wrote an app that has an action bar and 3 view pagers, now I finished the first pager, which is a Google map, and for the other 2, I currently just inflate them with a layout that has only a text view. The problem is that whenever I swipe to the 3rd view pager, and then swipe back, the app crashes, but switching between the first two is okay, except that the UI of the second pager seems to be affected by the google map UI. The code for the main activity is here:

public class LobbyActivity extends ActionBarActivity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

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

        // Set up the action bar

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        initializePager();

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for(int i=0; i<mSectionsPagerAdapter.getCount(); i++){
            actionBar.addTab(
                    actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
        }
    }

    private void initializePager() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, MapFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, SavedFragment.class.getName()));
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager = (ViewPager) findViewById(R.id.lobby_pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a fragment in the fragment container
            return this.fragments.get(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return "MAP VIEW";
                case 1:
                    return "YANK LIST";
                case 2:
                    return "SAVED YANKS";
            }
            return null;
        }
    }
}

and I have 3 fragment classes, they're almost the same, so I just show the map fragment:

public class MapFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        if (container == null)
            return null;

        return inflater.inflate(R.layout.fragment_lobby_map, container, false);

    }
}

And below is the layout for the main activity:

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

layout for map fragment:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"/>

The other two layouts for the remaining 2 fragments are just a linearLayout containing a textview.

When the app crashes, I get

    05-12 12:05:18.963      147-227/? W/MemoryDealer﹕ madvise(0x423c3000, 16384, MADV_REMOVE) returned Operation not supported on transport endpoint
05-12 12:05:18.963    9173-9173/com.yankteam.yank.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40cf2390)

Thanks a lot for reading this.

like image 307
J Freebird Avatar asked Dec 25 '22 09:12

J Freebird


1 Answers

I found this answer to call viewPager.setOffscreenPageLimit(3); and its working for me nicely.

like image 155
Bhupendra Avatar answered Jan 11 '23 06:01

Bhupendra