尝试使用PagerAdapter进行切换

尝试使用PagerAdapter进行切换

本文介绍了无法更改片段的标签错误-尝试使用PagerAdapter进行切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关该问题的最新文章,但没有找到有帮助的解决方案,我只是不明白问题是什么.该代码已经工作,但是似乎以下一些工作已经破坏了它,然后我删除了所做的每项更改,但仍然收到此错误.我希望有人对这个问题有更多的了解并能为我提供帮助.

I read the last our about this problem but didn't find a solution that helped, i just don't understand what the problem is.The code already worked, but it seems like some following work has destoryed it, then i deleted every change i've made but i get still this Error.I hope someone knows more about this problem and can help me.

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作为片段

This is the code in MainActivity where i use MyPagerAdapter for the 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;
    }
}

忘记发布vars

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

推荐答案

我认为此链接可以解释这里发生的事情.

由于数组中有3个项目,但是返回2个片段,其中1个重复使用的片段会通过&再次共享相同标签可能会导致此异常.因此,只需创建新实例,然后让适配器为您处理内存效率即可.

Since you have 3 items in the array, but returning 2 fragments, where 1 re-used fragment gets returned over & over again sharing the same tag likely causes this exception. So just make new instances, and let the adapter handle the memory efficiency for you.

也请不要使片段成为静态,否则它们的上下文(容器活动)将永远不会在内存中释放,从而导致大量活动的内存泄漏.

Also, do not make your fragments static, or else their context (container activity) can never be released in memory, causing huge activity memory leaks.

这篇关于无法更改片段的标签错误-尝试使用PagerAdapter进行切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:24