配置更改后无法恢复第二个fragmentStatePagerAdapter

时间:2015-07-05 04:51:36

标签: android-fragments android-pageradapter android-configchanges fragmentmanager

我有几天生病了。我的应用程序只包含一个包含多个主要片段的活动我有2个FramentStatePageAdapter,第一个在选择项目之后被第二个替换。但在方向更改后,应用程序崩溃,因为第二个适配器中的片段管理器无法获取嵌套片段。 我使用这种方法来替换主要片段:

public void replaceFragment(Fragment fragment, String tag){
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.container, fragment);
        fragmentTransaction.addToBackStack(tag);
        fragmentTransaction.commit();
        fragmentManager.executePendingTransactions();
        toolbar.setTitle(tag);
        updateUpButton();
    }

最初,我替换了一个名为HomeTabsFragment的片段,其中包含一个带有3个制表符和3个嵌套片段的FragmentStatePagerAdapter。 这就是我在HomeTabsFragment中创建适配器的方法:

playTubeFragmentAdapter = new HomeTabsAdapter(getChildFragmentManager());

这是第一个适配器:

public class HomeTabsAdapter extends FragmentStatePagerAdapter {

    public static final String[] mFragmentTitles = {"Music", "Video", "Playlist"};
    private HashMap<Integer, BaseFragment> mPageReferenceMap;

    public HomeTabsAdapter(FragmentManager fm) {
        super(fm);
        mPageReferenceMap = new HashMap<>();
    }

    @Override
    public Fragment getItem(int position){
        BaseFragment fragment = null;

        switch (position){
            case 0:
                fragment = MusicGenreFragment.newInstance();
                break;
            case 1:
                fragment = VideoCategoryFragment.newInstance();
                break;
            case 2:
                fragment = PlayListFragment.newInstance();
                break;

        }

        mPageReferenceMap.put(position, fragment);
        return fragment;
    }  
}

用户点击MusicGenreFragment的项目后,HomeTabsFragment被MusicGenreDetailFragment取代,后者包含一个在其中初始化的适配器:

MusicGenreDetailAdapter adapter = new   MusicGenreDetailAdapter(getChildFragmentManager(), genre);

这是MusicGenreDetailAdapter:

public class MusicGenreDetailAdapter extends FragmentStatePagerAdapter {
    private static final int NUMBER_OF_FRAGMENTS = 2;
    public static final String[] mFragmentTitles = {"Artists", "Songs"};
    private MMusicGenre mGenre;
    public MusicGenreDetailAdapter(FragmentManager fm, MMusicGenre genre) {
        super(fm);
        mGenre = genre;
    }
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new ArtistFragment();
           ((ArtistFragment)fragment).setChannelId(mGenre.getChannelId());
                break;
            case 1:
                fragment = new PopularVideoFragment();
            ((PopularVideoFragment)fragment).setChannelId(mGenre.getChannelId());
                break;
        }
        return fragment;
    }
    @Override
    public int getCount() {
        return NUMBER_OF_FRAGMENTS;
    }
    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
        super.restoreState(state, loader);
    }
}

我重写了方法 restoreState(Parcelable state,ClassLoader loader),配置更改后应用程序崩溃了。我试过调试,发现这个适配器的片段管理器中没有活动片段。如果我注释掉 restoreState 中的所有语句,则会显示第一个适配器。

这是logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rudy.youtubeplayer/com.rudy.youtubeplayer.ui.activity.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3760)
        at android.app.ActivityThread.access$900(ActivityThread.java:142)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:587)
        at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
        at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1318)
        at android.view.View.dispatchRestoreInstanceState(View.java:12872)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2652)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
        at android.view.View.restoreHierarchyState(View.java:12850)
        at android.support.v4.app.Fragment.restoreViewState(Fragment.java:466)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:981)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1929)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
        at android.app.Activity.performStart(Activity.java:5285)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)

    ....

有人可以帮我解决这个问题吗?

0 个答案:

没有答案