android错误:删除片段时“提交已调用”

时间:2013-09-09 11:44:07

标签: android android-fragments android-activity

我想实现一个包含片段的活动!当我点击Fragment1时,会调用Fragment2,当我点击Fragment2时,应该从屏幕上删除Fragment2! 我通过在onCreateView中调用Fragment2的LinearLayout的setOnclickListener来实现它,在我的onclicklistener上,我调用了

transaction.remove(myFragment);

transaction.commit();

但在那之后我遇到了这个错误:已经调用了commit 我该如何修复此错误,这是我的代码: 这是我的片段类

public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static LinearLayout l;
private android.support.v4.app.FragmentTransaction transaction;
private Fragment newFragment;

public void setArticleFragment(android.support.v4.app.FragmentTransaction transaction , Fragment newFragment) {
    this.transaction = transaction;
    this.newFragment = newFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }
    View v = inflater.inflate(R.layout.article_view, container , false);
    l = (LinearLayout) v.findViewById(R.id.transparentArea);
    if(l == null)
    l.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            transaction.remove(newFragment);
            transaction.commit();
        }
    });
    return v;
}
}

和我的mainAcitivity类

public class MainActivity extends FragmentActivity implements
    HeadlinesFragment.OnHeadlineSelectedListener{

HeadlinesFragment firstFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_articles);
    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }
        firstFragment = new HeadlinesFragment();
        firstFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }
}

public void onArticleSelected(int position) {
    ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager()
            .findFragmentById(R.id.article_fragment);

    if (articleFrag != null) {
        articleFrag.updateArticleView(position);

    } else {
        final ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);

        final FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        newFragment.setArticleFragment(transaction, newFragment);

        transaction.setCustomAnimations(R.anim.slidein, R.anim.slideout);
        transaction.add(R.id.fragment_container, newFragment);
        transaction.commit();

    }
}

3 个答案:

答案 0 :(得分:11)

不是重用传入的事务,而是创建一个删除Fragment2的新FragmentTransaction实例。

更容易将第一个片段事务添加到片段后台堆栈(例如addToBackStack(null)),然后在Fragment2中,只需使用FragmentManager popBackStack()弹出后台堆栈。

答案 1 :(得分:2)

开始新交易

fragTran = fragMan.beginTransaction();
fragTran.replace(R.id.frag_cont_two, fThree);
fragTran.addToBackStack(null);
fragTran.commit();

答案 2 :(得分:0)

我解决了这个问题 在调用commit()并再次替换片段后,您应该从

开始
fragmentTransaction = fragmentManager.beginTransaction();