我想使用相同的片段并用重置函数替换内容 但是当为每个标签返回相同的片段时,我得到以下错误:
无法更改片段FragmentRank的标签
这是我的代码:
class pagerAdapter extends FragmentPagerAdapter {
String[] tabs = {"1", "2", "3"};
FragmentRank rank;
public pagerAdapter(FragmentManager fm) {
super(fm);
rank = FragmentRank.newInstance();
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
return rank;
}
}
有什么想法吗?
logcat:
java.lang.IllegalStateException: Can't change tag of fragment FragmentRank{529c3b00 id=0x7f080044 android:switcher:2131230788:0}: was android:switcher:2131230788:0 now android:switcher:2131230788:1
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:398)
at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:16497)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
FragmentPager中的每个页面都需要有一个不同的Fragment,否则它无法在配置更改时重新创建它。它使用内部命名约定来标识每个片段并重新创建它。因此,每次调用getItem()时都会返回FragmentRank.newInstance()。
如果您想要更好地控制适配器内的Fragments,请使用以下方法:https://stackoverflow.com/a/15261142/535762
通过此,您可以重新附加您可能拥有的任何州信息。最好在你的片段的onResume()里面做这个