分配新的適配器崩潰時我有一個由FragmentStatePagerAdapter
應用程序啓動時填充的ViewPager
:應用到我的ViewPager
ViewPager mViewPager;
...
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new PageAdapter(getChildFragmentManager()));
//PageAdapter extends from FragmentStatePagerAdapter
}
這個,因爲它應該工作正常。
現在,當用戶更改應用程序的設置,我想一個新的適配器分配到ViewPager這樣的:
@Override
public void updateObserver(SETTING_KEY key)
{
mViewPager.setAdapter(new PageAdapter(getChildFragmentManager()));
Log.d("info", "updating");
}
不幸的是調用updateObserver
與此異常後,應用程序崩潰:
01-22 23:22:45.957:E/AndroidRuntime(13131):致命例外:主 01-22 23:22:45.957:E/AndroidRuntime(13131):java.lang.IllegalStateException:Fragement不再存在鍵f0:索引0 01-22 23:22:45.957 :E/AndroidRuntime(13131):在android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:575) 01-22 23:22:45.957:E/AndroidRuntime(13131):at android.support.v4 .app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211) 01-22 23:22:45.957:E/AndroidRuntime(13131):at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281) 01-22 23:22:45.957:E/AndroidRuntime(13131):at android.view.View.dispatchRestoreInstanceState(View.java:13188) 01-22 23:22:45.957:E/AndroidRuntime(13131):at android .view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2850) 01-22 23:22:45.957:E/AndroidRuntime(13131):at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2856) 01-22 23 :22:45.957:E/AndroidRuntime(13131):在android.view.View.resto在android.support.v4.app.Fragment.restoreViewState(Fragment.java:425) ...
():ReHierarchyState
如何在不使應用程序崩潰的情況下將新適配器分配給ViewPager
?
你試過這個嗎? http://stackoverflow.com/questions/12783571/android-viewpager-change-adapter 編輯:我看到這是從一年前。 – Peter