程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> fragment-Fragment already added 接手別人的代碼,出現這個問題,不知道如何 更改

fragment-Fragment already added 接手別人的代碼,出現這個問題,不知道如何 更改

編輯:編程綜合問答
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)

最佳回答:


這代碼寫的真亂 要是方便的話建議你刪了重寫,這代碼明顯無法實現滑動效果“//滑動動畫&&選擇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));
            }
        }
    }

這段刪了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved