Error with Fragments: "java.lang.IllegalStateException: FragmentManager is already executing transactions"


I'm having a small problem when creating fragments, it's that I get an Exception as if getSupportFragmentManager was already running.

The error:


E / AndroidRuntime: FATAL EXCEPTION: main                     Process: jhon.casique.baccus, PID: 30971                     java.lang.IllegalStateException: FragmentManager is already executing transactions                         at (                         at (                         at (                         at android.view.View.dispatchAttachedToWindow (                         at android.view.ViewGroup.dispatchAttachedToWindow (                         at android.view.ViewGroup.addViewInner (                         at android.view.ViewGroup.addView (                         at android.view.ViewGroup.addView (                         at android.view.ViewGroup.addView (                         at (                         at (                         at (                         at (                         at $ (                         at android.os.Handler.handleCallback (                         at android.os.Handler.dispatchMessage (                         at android.os.Looper.loop (                         at (                         at java.lang.reflect.Method.invoke (Native Method)                         at $ (                         at (


 public class WineryFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     super.onCreateView(inflater, container, savedInstanceState);

    View root = inflater.inflate(R.layout.activity_winery, container, false);

    //Creating wines.
    Wine bembibre =new Wine(
            "Dominio de Tares",
            "Este vino muestra toda la complejidad y la elegancia de la variedad Mencía. En fase visual luce un color rojo picota muy cobierto con tonalidades violáceas en el menisco. En nariz aparecen recuerdos frutales muy intensos de frutas rojas (frambuesa, cereza) y una potente ciruela negra asi como tonos florales de la gama de las rosas y violetas, vegetales muy elegantes y complementarios, hojarasca verde, tabaco y maderas aromáticas (sándalo) que le brindan un troque ciertamente perfumado",
            "El Bierzo",

    Wine vegaval =new Wine(
            "Miguel de Calatayud",
            "Este vino es para gente guay como Jhon ó Gabriela, el gordo no, el no es guay",

    //View for fragment add tabs.
    FragmentTabHost tabHost = (FragmentTabHost) root.findViewById(;
    tabHost.setup(getActivity(), getActivity().getSupportFragmentManager(),;

    //Adding the first tab.
    Bundle arguments = new Bundle();
    arguments.putSerializable(WineFragment.ARG_WINE, bembibre);
    tabHost.addTab(tabHost.newTabSpec(bembibre.getName()).setIndicator(bembibre.getName()), WineryFragment.class, arguments);

    //Adding the second tab
    arguments = new Bundle();
    arguments.putSerializable(WineFragment.ARG_WINE, vegaval);
    tabHost.addTab(tabHost.newTabSpec(vegaval.getName()).setIndicator(vegaval.getName()), WineryFragment.class, arguments);

    return root;

And this is the code of the activity

package jhon.casique.baccus.controller.activity;


import jhon.casique.baccus.controller.fragments.WineryFragment;

public class WineryActivity extends FragmentContainerActivity {
    protected Fragment createFragment() {
         return new WineryFragment();
asked by Jhon 22.10.2016 в 18:15

1 answer


The truth is that I have never used any FragmentTabHost but after looking at the Android documentation for this method here I've seen that it never calls the same fragment twice.

Actually in your code you are repeating the same class for two different tabs:

tabHost.addTab(tabHost.newTabSpec(bembibre.getName()).setIndicator(bembibre.getName()), WineryFragment.class, arguments);

since you refer twice to WineryFragment.class .

I can deduce, therefore, that the problem is there since your error also indicates that FragmentManager is already running, so it would make sense to give you the error in the second tabHost.addTab since you have created one for the first wine.

My proposal is that you create a new Fragment for each wine and then call from a common class all the different tabs that you have created in different classes using Fragments with the tabHost.addTab method.

I hope this solves your problem.

answered by 22.10.2016 в 18:39