Android:FragmentManager在切换片段时已经在执行事务

时间:2018-02-03 18:29:01

标签: android android-fragments

当我尝试在两个片段之间切换时,我有这个错误,我不知道如何解决它:

错误:

java.lang.IllegalStateException: FragmentManager is already executing transactions
                      at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2177)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2233)
                      at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:781)
                      at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:289)
                      at android.view.View.dispatchAttachedToWindow(View.java:17445)
                      at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3326)
                      at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3333)
                      at android.view.ViewGroup.addViewInner(ViewGroup.java:4977)
                      at android.view.ViewGroup.addView(ViewGroup.java:4768)
                      at android.view.ViewGroup.addView(ViewGroup.java:4708)
                      at android.view.ViewGroup.addView(ViewGroup.java:4681)

我的代码:

transaction = getFragmentManager().beginTransaction();

    linearScore = (LinearLayout)view.findViewById(R.id.linearScore);
    linearScore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        fragment_score frag_score= new fragment_score();
            switch_fragment(frag_score,"fragment_score");
        }
    });
    linearReglage = (LinearLayout)view.findViewById(R.id.linearReglage);
    linearReglage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fragment_reglage frag_reglage= new fragment_reglage();
            switch_fragment(frag_reglage,"fragment_reglage");
        }
    });
    RoundedImageView roundedImageView =(RoundedImageView)view.findViewById(R.id.futurImage);
    roundedImageView.setImageBitmap(((Main)getActivity()).getBmImage());
    return view;
}

public void switch_fragment(Fragment fragment, String tag ){
    transaction.add(R.id.fragment_container, fragment,tag);
    transaction.addToBackStack(tag);
    transaction.commit();
}

有关信息,我已经在另一个片段中有另一个tabhost,我不知道它是否是链接

感谢回复

0 个答案:

没有答案