@火蚁 你好,想跟你请教个问题:
一个Activity里面有个FragmentTabHost,有4个Fragment ABCD, B是里面是一个ViewPager,放的是Fragment,A切换到B,B切换到A,然后A切换到B,就报错了:
java.lang.IllegalStateException: Fragment no longer exists for key f0: index 0 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:673) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1319) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.view.View.dispatchRestoreInstanceState(View.java:13740) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2887) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.view.View.restoreHierarchyState(View.java:13718) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.Fragment.restoreViewState(Fragment.java:468) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1426) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:728) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 11-27 16:21:27.820 13962-13962/com.xiaolu123.video E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)报错位置是在B的OnViewCreated给ViewPager setAdapter,
我的Adapter:
public class RankPagerAdapter extends FragmentStatePagerAdapter { private final ArrayList<ViewPageInfo> mTabs = new ArrayList<>(); private Context mContext; public RankPagerAdapter(FragmentManager fm, Context context) { super(fm); mContext = context; } @Override public Fragment getItem(int position) { ViewPageInfo info = mTabs.get(position); return Fragment.instantiate(mContext, info.clss.getName(), info.args); } @Override public int getCount() { return mTabs.size(); } public void addTab(String title, String tag, Class<?> clss, Bundle args) { ViewPageInfo viewPageInfo = new ViewPageInfo(title, tag, clss, args); mTabs.add(viewPageInfo); } @Override public CharSequence getPageTitle(int position) { return mTabs.get(position).title; } }
基本就是按照开源中国Android版的代码写的,但还是报错,求指点
基本上可以确定是FragmentB的问题,看看是不是你在FragmentB的viewpage中重复添加了相同的fragment
能加下你QQ么,请教一下问题有什么问题,留言或者讨论区提问就好了,qq一般不上这个问题有解决吗?解决方法,重写以下方法,返回空即可。
@Override
public Parcelable saveState() {
return null;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。