开发者社区> 问答> 正文

Android Viewpager报错Fragment no longer ex?报错

@火蚁 你好,想跟你请教个问题:

一个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版的代码写的,但还是报错,求指点

展开
收起
爱吃鱼的程序员 2020-06-10 10:27:27 1551 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    基本上可以确定是FragmentB的问题,看看是不是你在FragmentB的viewpage中重复添加了相同的fragment

    能加下你QQ么,请教一下问题有什么问题,留言或者讨论区提问就好了,qq一般不上这个问题有解决吗?

    解决方法,重写以下方法,返回空即可。

    @Override  

    public Parcelable saveState() {  

        return null;  

    2020-06-10 10:27:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载