gpt4 book ai didi

android - 从 viewpager 向后滑动会导致应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:19 27 4
gpt4 key购买 nike

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

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 个 fragment 类,它们几乎相同,所以我只显示 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);

}
}

下面是主要 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"
/>

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

其余 2 个 fragment 的另外两个布局只是一个包含 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)

非常感谢阅读本文。

最佳答案

我找到了调用 viewPager.setOffscreenPageLimit(3) 的答案;它对我很有用。

关于android - 从 viewpager 向后滑动会导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23616945/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com