ViewPager嵌套带ViewPager的Banner,来回切换Banner数据丢失

数据丢失解决方法:
其实是Fragment嵌套了Fragment的问题,外层的Fragment是调用
   
   
NewMainPagerAdapter adapter = new NewMainPagerAdapter(getFragmentManager(), viewpagerFragmentList, mTitles);
而内层是要调用
   
   
BannerPagerAdapter mBannerPagerAdapter = new BannerPagerAdapter(fragment, mNewsMain.banners);
    
    
public BannerPagerAdapter(Fragment fm, List<NewsMain.Banner> banner) {
super(fm.getChildFragmentManager());
mBanner = banner;
}


修改完成后数据不会丢失了,但是来回切换几下,又会crash,报错如下
   
   
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: FATAL EXCEPTION: main
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: java.lang.NullPointerException
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentState.instantiate(Fragment.java:94)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1747)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.Fragment.performCreate(Fragment.java:1684)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:682)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:474)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v13.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:494)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1584)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout.selectTab(TabLayout.java:807)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout.selectTab(TabLayout.java:779)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1039)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:620)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.view.View.performClick(View.java:4204)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-15 05:52:24.715 2976-2976/com.saidian.zuqiukong.debug E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

解决方法:
重写嵌套Fragment的onDetach方法(即调用了 getChildFragmentManager() 的Fragment
   
   
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
 
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值