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

4

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 android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1649)                         at android.support.v4.app.FragmentManagerImpl.executePendingTransactions (FragmentManager.java:589)                         at android.support.v4.app.FragmentTabHost.onAttachedToWindow (FragmentTabHost.java:285)                         at android.view.View.dispatchAttachedToWindow (View.java:14607)                         at android.view.ViewGroup.dispatchAttachedToWindow (ViewGroup.java:2837)                         at android.view.ViewGroup.addViewInner (ViewGroup.java:4349)                         at android.view.ViewGroup.addView (ViewGroup.java:4146)                         at android.view.ViewGroup.addView (ViewGroup.java:4087)                         at android.view.ViewGroup.addView (ViewGroup.java:4060)                         at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1124)                         at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1290)                         at android.support.v4.app.BackStackRecord.run (BackStackRecord.java:801)                         at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1677)                         at android.support.v4.app.FragmentManagerImpl $ 1.run (FragmentManager.java:536)                         at android.os.Handler.handleCallback (Handler.java:739)                         at android.os.Handler.dispatchMessage (Handler.java:95)                         at android.os.Looper.loop (Looper.java:234)                         at android.app.ActivityThread.main (ActivityThread.java:5526)                         at java.lang.reflect.Method.invoke (Native Method)                         at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:726)                         at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

MY CODE

 public class WineryFragment extends Fragment {
    @Override
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(
            "Bembibre",
            "Tinto",
            R.drawable.bembibre,
            "Dominio de Tares",
            "http://www.dominiodetares.com/portfolio/bembibre/",
            "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",
            5);
    bembibre.addGrape("Mencía");

    Wine vegaval =new Wine(
            "Vegaval",
            "Tinto",
            R.drawable.vegaval,
            "Miguel de Calatayud",
            "http://www.vegaval.com/es",
            "Este vino es para gente guay como Jhon ó Gabriela, el gordo no, el no es guay",
            "Valdepeñas",
            4);
    vegaval.addGrape("Tempranillo");

    //View for fragment add tabs.
    FragmentTabHost tabHost = (FragmentTabHost) root.findViewById(android.R.id.tabhost);
    tabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), android.R.id.tabcontent);


    //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 android.support.v4.app.Fragment;

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

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

1 answer

1

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