Android的FragmentTransaction承诺已经调

Android的FragmentTransaction承诺已经调

本文介绍了Android的FragmentTransaction承诺已经调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的错误:

java.lang.IllegalStateException:已提交名为

我的code:

 最后FragmentTransaction fragmentTransaction = getFragmentManager()调用BeginTransaction()。f1_fragment =新F1_Fragments();
f2_fragment =新F2_Fragments();listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
    @覆盖
    公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            parent.getItemAtPosition(位置);            如果(位置== 0){
                fragmentTransaction.replace(android.R.id.content,f1_fragment);
            }其他{
                fragmentTransaction.replace(android.R.id.content,f2_fragment);
            }            fragmentTransaction.addToBackStack(NULL);
            fragmentTransaction.commit();
        }
    });


解决方案

您已经开始的 OnItemClickListener 外FragmentTransaction。因此,你正试图每次用户在你的ListView点击一个项目的时间提交()单FragmentTransaction。

您需要在每次开始一个新FragmentTransaction你打算执行任意数量的片段操作。

一个简单的修复是这样的:

  f1_fragment =新F1_Fragments();
f2_fragment =新F2_Fragments();listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){    @覆盖
    公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
        FragmentTransaction fragmentTransaction = getFragmentManager()调用BeginTransaction()。        parent.getItemAtPosition(位置);        如果(位置== 0){
            fragmentTransaction.replace(android.R.id.content,f1_fragment);
        }其他{
            fragmentTransaction.replace(android.R.id.content,f2_fragment);
        }        fragmentTransaction.addToBackStack(NULL);
        fragmentTransaction.commit();
    }
});

My ERROR :

java.lang.IllegalStateException: commit already called

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();
        }
    });
解决方案

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.

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

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();
    }
});

这篇关于Android的FragmentTransaction承诺已经调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:32