Android FragmentTransaction 提交已调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24561874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:25:17  来源:igfitidea点击:

Android FragmentTransaction commit already called

androidandroid-fragmentsfragmenttransaction

提问by JPs

My ERROR :

我的错误:

java.lang.IllegalStateException: commit already called

java.lang.IllegalStateException:提交已调用

My CODE:

我的代码:

final FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();

f1_fragment  = new F1_Fragments();
f2_fragment = new F2_Fragments();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            parent.getItemAtPosition(position);

            if(position==0){
                fragmentTransaction.replace(android.R.id.content, f1_fragment);
            }else{
                fragmentTransaction.replace(android.R.id.content, f2_fragment);
            }

            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

回答by Bryan Herbst

You are beginning the FragmentTransaction outside of the OnItemClickListener. Thus you are attempting to commit()a single FragmentTransaction every time the user clicks an item in your ListView.

您正在OnItemClickListener. 因此,commit()每次用户单击 ListView 中的项目时,您都会尝试执行单个 FragmentTransaction。

You need to begin a new FragmentTransaction every time you intend to perform any number of Fragment operations.

每次您打算执行任意数量的 Fragment 操作时,您都需要开始一个新的 FragmentTransaction。

A simple fix would look like this:

一个简单的修复看起来像这样:

f1_fragment  = new F1_Fragments();
f2_fragment = new F2_Fragments();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();

        parent.getItemAtPosition(position);

        if(position==0){
            fragmentTransaction.replace(android.R.id.content, f1_fragment);
        }else{
            fragmentTransaction.replace(android.R.id.content, f2_fragment);
        }

        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
});

回答by idris y?ld?z

you can use this method for replace fragment with each other just call this

您可以使用此方法相互替换片段,只需调用此方法

do those are global

做那些是全球性的

YourFragment1 frg1 = new   YourFragment1 ();    
YourFragment2 frg1 = new   YourFragment2 (); 

And then call it by

然后调用它

openFragment(frg1); 

or

或者

  openFragment(frg2);

OpenFragment:

开放片段:

  private void openFragment(final Fragment fragment)   {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();        
    transaction.replace(R.id.container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();

}

回答by Punithapriya

Look at this scenario

看看这个场景

I need to add new fragment on every list item click. Just initialise fragment transactionon every item click.

我需要在每个列表项点击上添加新片段。只需在每次单击项目时初始化片段事务

switch (v.getId()) {

                case R.id.tvaddExpense:

                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.add(R.id.containerFucntionsList, new Fragment1());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                    break;

                case R.id.relEvents:
                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.containerFucntionsList, new Fragment2());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                    break;
            } 

回答by Bakshish Singh

The Error

错误

java.lang.IllegalStateException: commit already called

java.lang.IllegalStateException:提交已调用

shows that the FragmentTransactionhas been completed after calling commit()the first time and you are again calling commit()which tends to complete it once again. Hence it makes an Illegal state for the FragmentTransaction.

显示在第一次FragmentTransaction调用后已经完成commit(),你再次调用commit()它往往会再次完成它。因此,它使FragmentTransaction.

As per your code, you are using the same FragmentTransactionfor changing fragments. However, after the first commit()call, the FragmentTransactionhas completed and you need to begin it again to perform any operation on Fragments.

根据您的代码,您使用相同的代码FragmentTransaction来更改片段。但是,在第一次commit()调用之后,FragmentTransaction已完成,您需要再次开始以对 执行任何操作Fragments

You can change your ClickListneras:

您可以将您的更改ClickListner为:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            fragmentTransaction = getSupportFragmentManager().beginTransaction();   

            parent.getItemAtPosition(position);

            if(position==0){
                fragmentTransaction.replace(android.R.id.content, f1_fragment);
            }else{
                fragmentTransaction.replace(android.R.id.content, f2_fragment);
            }

            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

I hope it clears your doubt.

我希望它能消除你的疑惑。

回答by Hiren Patel

I had same issue and I have solved by creating new instanceof FragmentTransaction.

我有同样的问题,我已经解决了创建新实例FragmentTransaction

Just add everytimebelow line before add/ replacefragment.

添加/替换片段之前,只需在行下方添加

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

Hope this would help you.

希望这会帮助你。

回答by Abolfazl Karimi

You must define a FragmentTransAction object separately each time you add a fragment:

每次添加片段时都必须单独定义一个 FragmentTransAction 对象:

     switch (position){
                case 0:
                    FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction1.replace(R.id.parentOfMenu, new FragmentHome());
                    fragmentTransaction1.commit();

                    break;
                case 1:
                    FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction2.replace(R.id.parentOfMenu, new FragmentMedia());
                    fragmentTransaction2.commit();

                    break;

                case 2:
                    FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction3.replace(R.id.parentOfMenu, new FragmentChart());
                    fragmentTransaction3.commit();

                    break;

                case 3:
                    FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction4.replace(R.id.parentOfMenu, new FragmentProfile());
                    fragmentTransaction4.commit();

                    break;
            }

回答by Fazal

I solved the issue after calling commit()and again replacing the fragment you should start from

我在调用commit()并再次替换你应该开始的片段后解决了这个问题

fragmentTransaction = fragmentManager.beginTransaction();

if(position==0){
     fragmentTransaction.replace(android.R.id.content, f1_fragment);
 else{
     fragmentTransaction.replace(android.R.id.content, f2_fragment);
 }

 fragmentTransaction.addToBackStack(null);
 fragmentTransaction.commit();