qq_23970651
qq_23970651
2015-07-30 09:00
采纳率: 50%
浏览 7.4k

Fragment already added 接手别人的代码,出现这个问题,不知道如何 更改

接手别人的代码,出现这个问题,不知道如何改,求助。界面是有四个fragment,通过底部的四个图片切换四个fragment,出现这个问题,主要代码如下,求帮助:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    Intent intent = getIntent();

    page_num = intent.getIntExtra("page_num", 0);

    setContentView(R.layout.main_activity);

    ButterKnife.inject(this);

    setSwipeBackEnable(false);

    queue = VolleyQuery.getQueue(this);

    intidrawable();

    getLocal();

           intiView(page_num);

    //设置导航点击方法
    nav_mshop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            slider_animate(0);

        }
    });

    nav_apply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            slider_animate(1);
        }
    });

    nav_join.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            slider_animate(2);
        }
    });

    nav_mine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            slider_animate(3);

        }
    });

    if (page_num == 0) {
        getNewVersion("auto");
    }

}

//滑动动画&&选择fragment
private void slider_animate(int postion) {

    for (int i = 0; i < 4; i++) {

        LinearLayout linearLayout = (LinearLayout) nav_container.getChildAt(i);

        TextView textView = (TextView) linearLayout.getChildAt(1);

        ImageView imageView = (ImageView) linearLayout.getChildAt(0);

        if (postion == i) {
            textView.setTextColor(getResources().getColorStateList(R.color.nav_text_pink));
            imageView.setImageDrawable(drawables[i][0]);

        } else {
            textView.setTextColor(getResources().getColorStateList(R.color.nav_text_gra));

            imageView.setImageDrawable(drawables[i][1]);
        }
    }


    if (mfilst.get(postion) == null) {
        switch (postion) {
            case 0:
                MshopFragment mshopFragment = new MshopFragment();
                mfilst.put(postion, mshopFragment);
                break;
            case 1:
                ApplyFragment applyFragment = new ApplyFragment();
                mfilst.put(postion, applyFragment);

                break;
            case 2:
                LeagueFragment leagueFragment = new LeagueFragment();
                mfilst.put(postion, leagueFragment);

                break;
            case 3:
                MyFragment myFragment = new MyFragment();
                mfilst.put(postion, myFragment);

                break;
        }
    }


    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (!mfilst.get(postion).isAdded()) {

        try {
            fragmentTransaction.add(R.id.main_container, mfilst.get(postion), String.valueOf(postion));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    for (int i = 0; i < 4; i++) {
        if (i != postion) {
            if (mfilst.get(i) != null && mfilst.get(i).isAdded()) {
                fragmentTransaction.hide(mfilst.get(i));
            }
        }
    }

    fragmentTransaction.show(mfilst.get(postion));
    fragmentTransaction.commit();
}

boolean addList = false;

@Override
protected void onResume() {

    super.onResume();

    this.addList = false;

}

@Override
protected void onPause() {

    super.onPause();

}


private void intiView(int page_num) {
    slider_animate(page_num);
}

private void intidrawable() {
    drawables[0][0] = getResources().getDrawable(R.mipmap.main_mshop_bottom_pink);
    drawables[0][1] = getResources().getDrawable(R.mipmap.main_mshop_bottom_gra);
    drawables[1][0] = getResources().getDrawable(R.mipmap.main_apply_bottom_pink);
    drawables[1][1] = getResources().getDrawable(R.mipmap.main_apply_bottom_gra);
    drawables[2][0] = getResources().getDrawable(R.mipmap.main_join_bottom_pink);
    drawables[2][1] = getResources().getDrawable(R.mipmap.main_join_bottom_gra);
    drawables[3][0] = getResources().getDrawable(R.mipmap.main_mine_bottom_pink);
    drawables[3][1] = getResources().getDrawable(R.mipmap.main_mine_bottom_gra);
}

错误日志:
Caused by: java.lang.IllegalStateException: Fragment already added: LeagueFragment{42e18a48 #1 id=0x7f0a00bb 2}
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:673)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4881)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • u012975705
    boy_nihao 2015-07-31 02:17
    已采纳

    这代码写的真乱 要是方便的话建议你删了重写,这代码明显无法实现滑动效果“//滑动动画&&选择fragment,逻辑好乱
    这写法界面只能显示一个fragment 改吧fragmentTransaction.add改为//滑动动画&&选择fragmentTransaction.replace

     for (int i = 0; i < 4; i++) {
            if (i != postion) {
                if (mfilst.get(i) != null && mfilst.get(i).isAdded()) {
                    fragmentTransaction.hide(mfilst.get(i));
                }
            }
        }
    

    这段删了

    点赞 评论
  • Arnold9009
    Arnold9009 2015-07-31 05:48

    直接replace就好,不用add/remove那么麻烦

    点赞 评论
  • gao_chun
    gao_chun 2015-08-01 03:40

    我文章这个例子你可以参考参考:http://blog.csdn.net/gao_chun/article/details/37903673

    点赞 评论
  • djl12rg
    djl12rg 2016-03-18 06:53

    然后呢 怎么处理 。。。。。。。我擦嘞

    点赞 评论
  • stone_2323
    stone_2323 2016-06-16 10:10

    仅仅把add改为replace解决不了问题的,快速切换依然会出Fragment already added

    点赞 评论
  • say_from_wen
    say_from_wen 2017-07-07 10:09

    楼主知道这方面的问题吗?我也遇到了,这个问题是不是不会发生崩溃,我自己测试没出现过这种情况啊,上线才有的额

    点赞 评论

相关推荐