从viewpager向后滑动会导致应用崩溃

时间:2014-05-12 19:08:17

标签: android google-maps android-fragments android-actionbar android-viewpager

我写了一个有一个操作栏和3个视图寻呼机的应用程序,现在我完成了第一个寻呼机,这是一个谷歌地图,而对于另外两个,我目前只是用一个只有文本视图的布局来充气它们。问题是每当我滑动到第三个视图寻呼机,然后向后滑动,应用程序崩溃,但在前两个之间切换是可以的,除了第二个寻呼机的UI似乎受到谷歌地图UI的影响。主要活动的代码在这里:

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;
        }
    }
}

我有3个片段类,它们几乎相同,所以我只显示地图片段:

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);

    }
}

以下是主要活动的布局:

<?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"
    />

地图片段的布局:

<?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"/>

其余两个片段的其他两个布局只是一个包含textview的linearLayout。

当应用程序崩溃时,我会

    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)

非常感谢您阅读本文。

1 个答案:

答案 0 :(得分:8)

我发现这个答案叫做viewPager.setOffscreenPageLimit(3);它很适合我。