片段已存在于android中

时间:2015-06-09 20:12:56

标签: android-fragments android-fragmentactivity

**我已经在这里设置了三个onClickListener并尝试相应地替换片段...但每当我尝试点击今天的onClickListener时,应用程序崩溃并发送"片段已经存在"并指向今天onClickListener的fragment.setArguments(b)....但与其他两个每周和每月的onClickListener完美配合...

**任何人请帮助**

today.setOnClickListener(new View.OnClickListener(){

Where-Object

1 个答案:

答案 0 :(得分:0)

@覆盖

        public void onClick(View v) {


            Bundle b = new Bundle();
            b.putString("url", "");
            //Add this to create the new instance of the fragment 
            fragment = new TodayFragment();
            fragment.setArguments(b);

            fm.beginTransaction().replace(R.id.container, fragment).commit();
            fm.executePendingTransactions();
            today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
            monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
            weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));


        }
    });
    {

        weekly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment = new TodayFragment();
                Bundle b = new Bundle();
                b.putString("url", "");
                fragment.setArguments(b);
                fm.executePendingTransactions();
                fm.beginTransaction().replace(R.id.container, fragment).commit();
                weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
                monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
                today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));

            }
        });
    }

    monthly.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragment = new TodayFragment();
            Bundle b = new Bundle();
            b.putString("url", "");
            fragment.setArguments(b);
            fm.executePendingTransactions();
            fm.beginTransaction().replace(R.id.container, fragment).commit();
            monthly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_950));
            weekly.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
            today.setBackgroundColor(getResources().getColor(R.color.material_blue_grey_800));
        }
    });

在您的代码中创建一个今日片段实例,它将解决您的问题。