在片段内使用viewPager时,片段崩溃

时间:2018-07-11 07:31:37

标签: android android-fragments exception android-viewpager

我在Fragment中使用ViewPager。我正在关注Crashlytics中的崩溃报告。请帮助解决该问题的方法

Fatal Exception: java.lang.IllegalStateException
Child FragmentManager of MovieFragmentBase{9fa4a22 #1 id=0x7f0a0468 MovieFragmentBase} was not destroyed and this fragment is not retaining instance
android.support.v4.app.Fragment.performDetach (Unknown Source)
android.support.v4.app.FragmentManagerImpl.moveToState (Unknown Source)
android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (Unknown Source)
android.support.v4.app.BackStackRecord.executePopOps (Unknown Source)
android.support.v4.app.FragmentManagerImpl.executeOps (Unknown Source)
android.support.v4.app.FragmentManagerImpl.executeOpsTogether (Unknown Source)
android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (Unknown Source)
android.support.v4.app.FragmentManagerImpl.execPendingActions (Unknown Source)
android.support.v4.app.FragmentManagerImpl$1.run (Unknown Source)
android.os.Handler.handleCallback (Handler.java:822)
android.os.Handler.dispatchMessage (Handler.java:104)
android.os.Looper.loop (Looper.java:207)
android.app.ActivityThread.main (ActivityThread.java:5736)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:910)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:771)

请检查我在MovieFragmentBase内部使用的viewPagerAdapter代码。

class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }


        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }


    }

1 个答案:

答案 0 :(得分:0)

尝试在setRetainInstance(true);的{​​{1}}中添加onCreate(),可能会为您提供帮助

有关MovieFragmentBase的更多信息,请点击以下链接

Further understanding setRetainInstance(true)