gpt4 book ai didi

android - 无法更改 fragment 错误的标记 - 尝试使用 PagerAdapter 进行切换

转载 作者:行者123 更新时间:2023-11-29 14:22:58 24 4
gpt4 key购买 nike

我阅读了关于这个问题的最后一篇文章,但没有找到有用的解决方案,我只是不明白问题出在哪里。代码已经工作,但似乎一些后续工作已经破坏了它,然后我删除了我所做的每一个更改,但我仍然得到这个错误。我希望有人知道更多关于这个问题并能帮助我。

06-22 22:59:01.966: E/AndroidRuntime(4701): java.lang.IllegalStateException: Can't change tag of fragment CardFragment{426ce8a0 #1 id=0x7f0e001f android:switcher:2131623967:1}: was android:switcher:2131623967:1 now android:switcher:2131623967:2
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.app.BackStackRecord.doAddOp(BackStackRecord.java:399)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.app.BackStackRecord.add(BackStackRecord.java:390)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v13.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer.doFrame(Choreographer.java:543)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.os.Handler.handleCallback(Handler.java:733)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.os.Looper.loop(Looper.java:136)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-22 22:59:01.966: E/AndroidRuntime(4701): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 22:59:01.966: E/AndroidRuntime(4701): at java.lang.reflect.Method.invoke(Method.java:515)
06-22 22:59:01.966: E/AndroidRuntime(4701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-22 22:59:01.966: E/AndroidRuntime(4701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-22 22:59:01.966: E/AndroidRuntime(4701): at dalvik.system.NativeStart.main(Native Method)

这是 MainActivity 中的代码,我将 MyPagerAdapter 用于 Fragments

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(R.string.app_name);
getActionBar().setIcon(R.drawable.ic_action_logo);

setContentView(R.layout.main);
getActionBar().setDisplayUseLogoEnabled(true);

tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager1);

mColors = new int[]{
R.color.myGreen,
R.color.blue,
R.color.orange,
R.color.red,
R.color.green,
};

adapter = new MyPagerAdapter(getFragmentManager());
pager.setAdapter(adapter);
int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);

//use the database
openDB();
}

@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}

@Override
public void onPause(){
super.onPause();
closeDB();
}

private void closeDB() {
myDB.close();
}

private void openDB() {
myDB = new DBAdapter(this);
myDB.open();
}

public class MyPagerAdapter extends FragmentPagerAdapter {

private final String[] TITLES;

public MyPagerAdapter(FragmentManager fm) {
super(fm);
TITLES = getResources().getStringArray(R.array.tab_titles);
}

@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}

@Override
public int getCount() {
return TITLES.length;
}

@Override
public Fragment getItem(int position) {
switch(position){
case 0: cardF0 = new CardFragment(TITLES[0], mColors[0]); return cardF0;
case 1: cardF1 = new CardFragment(TITLES[1], mColors[0]); return cardF1;
case 2: return cardF1;
}
return cardF0;
}
}

编辑:忘记发布变量

    private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private static CardFragment cardF0;
private static CardFragment cardF1;

最佳答案

I think this link can explain whats happening here.

It's probably because you're adding the same fragment instance three times to the list. You should create a new instance for each page.

由于您在数组中有 3 个项目,但返回 2 个 fragment ,其中 1 个重复使用的 fragment 一遍又一遍地返回,共享相同的标签可能会导致此异常。所以只需创建新实例,让适配器为您处理内存效率。

此外,不要让您的 fragment 成为静态的,否则它们的上下文(容器 Activity )永远无法在内存中释放,从而导致巨大的 Activity 内存泄漏。

关于android - 无法更改 fragment 错误的标记 - 尝试使用 PagerAdapter 进行切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com