How to fix: Fragment x is not currently in the FragmentManager in ANDROID

1

I'm with an application that works with fragments and when I'm in the fragment called ReservaOcupaciónFragment and I press the Home button of the phone (which is the one that leaves the app) it gives me an exception and therefore an error.

The error is as follows:

This error only occurs in this fragment, since in the previous one and the following everything works correctly. What I do not know is why he only gives me this exception in this passage. I add the .java:

@SuppressLint("NewApi")
public class ReservaFragment extends Fragment implements OnClickListener,
    OnItemClickListener {

/** The root view. */
private View rootView;

/** The lista recursos. */
private ListView listaRecursos;

/** The button fecha. */
private Button buttonFecha;

/** The button avanzar. */
private Button buttonAvanzar;

/** The button retroceso. */
private Button buttonRetroceso;

/** The context. */
private static ReservaFragment context;

private Utilidades utilidades;

/** The datos. */
private ReservaBean datos;

public static android.support.v4.app.Fragment newInstance(Bundle arguments) {
    context = new ReservaFragment();
    if (arguments != null) {
        context.setArguments(arguments);
    }
    return context;
}

public ReservaFragment() {
    datos = new ReservaBean();
    utilidades = new Utilidades();
}

/*
 * (non-Javadoc)
 * 
 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater,
 * android.view.ViewGroup, android.os.Bundle)
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

     super.onCreateView(inflater, container, savedInstanceState); 
    rootView = inflater.inflate(R.layout.c_1_0_reserva_fragmet, container,
            false);

    // cargamos metodos
    init();
    bindComponents();

    return rootView;
}

/*
 * (non-Javadoc)
 * 
 * @see android.view.View.OnClickListener#onClick(android.view.View)
 */
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_fecha_recurso:
        showDatePickerDialog(v);
        break;

    case R.id.button_retroceder:
        Toast.makeText(rootView.getContext(),
                "Funcionalidad no disponible", Toast.LENGTH_SHORT).show();
        break;
    case R.id.button_avanzar:
        if (datos.getFechaSeleccionada() != null) {
            if (datos.getCodigoTipoRecurso() != -1) {
                OcupacionRecursoThread recurso = new OcupacionRecursoThread(
                        datosInService());
                if (recurso.getRecursos() != null && recurso.getRecursos().size() > 0) {

                    ReservaRequest request = new ReservaRequest();
                    request.setCodigoUsuario(datos.getCodUSuario());
                    request.setFechaReserva(datos.getFechaSeleccionada());
                    request.setTipoRecurso(obtenerTipoRecurso(datos.getCodigoTipoRecurso()));
                    request.setListaRecursos(recurso.getRecursos());
                    request.setUrl(datos.getUrl());
                    accesoRecursos(request);
                } else { 
                    Toast.makeText(rootView.getContext(),"Combinación de fecha y "
                                + "tipo de recurso incorrecto",
                            Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(rootView.getContext(),
                        "Tipo de recurso obligatorio", Toast.LENGTH_LONG)
                        .show();
            }
        } else {
            Toast.makeText(rootView.getContext(), "Fecha obligatoria",
                    Toast.LENGTH_LONG).show();
        }

    default:
        break;
    }
}

/*
 * (non-Javadoc)
 * 
 * @see
 * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
 * .AdapterView, android.view.View, int, long)
 */
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    for (int count = 0; count < listaRecursos.getAdapter().getCount(); count++) {
        if (position == count) {
            listaRecursos.getChildAt(count).setBackgroundColor(Color.CYAN);
            ListaDto lista = (ListaDto) listaRecursos.getAdapter().getItem(position);
            datos.setCodigoTipoRecurso(lista.getId());
        } else {
            listaRecursos.getChildAt(count).setBackgroundColor(Color.TRANSPARENT);
        }
    }

}

/**
 * Inits the.
 */
private void init() {
    TipoRecursoThread tipoRecursoRunnable = new TipoRecursoThread(datosInService());
    datos.setTiposRecursos(tipoRecursoRunnable.getTiposDeRecurso());

}

/**
 * Bind components.
 */
private void bindComponents() {
    //context = this;

    listaRecursos = (ListView) rootView
            .findViewById(R.id.list_tipos_recursos);
    ListaAdapter adapter = new ListaAdapter(this.getActivity(),
            obtenerLista());
    listaRecursos.setAdapter(adapter);
    listaRecursos.setOnItemClickListener(this);

    buttonFecha = (Button) rootView.findViewById(R.id.button_fecha_recurso);
    buttonFecha.setOnClickListener(this);
    buttonFecha.setText(utilidades.dateToString(new Date()));
    datos.setFechaSeleccionada(new Date());

    buttonRetroceso = (Button) rootView
            .findViewById(R.id.button_retroceder);
    buttonRetroceso.setOnClickListener(this);

    buttonAvanzar = (Button) rootView.findViewById(R.id.button_avanzar);
    buttonAvanzar.setOnClickListener(this);
}

/**
 * Obtener lista.
 * 
 * @return the list
 */
private List<ListaDto> obtenerLista() {
    List<ListaDto> lista = new ArrayList<ListaDto>();
    for (TipoRecursoDto tipoRecurso : datos.getTiposRecursos()) {
        ListaDto dto = new ListaDto();
        dto.setId(tipoRecurso.getCodigo());
        dto.setNombre(tipoRecurso.getDescripcion());

        lista.add(dto);
    }

    return lista;
}

/**
 * Obtener tipo recurso.
 * 
 * @param codigo
 *            the codigo
 * @return the tipo recurso dto
 */
private TipoRecursoDto obtenerTipoRecurso(int codigo) {
    for (TipoRecursoDto tiRecurso : datos.getTiposRecursos()) {
        if (tiRecurso.getCodigo() == codigo) {
            return tiRecurso;
        }
    }

    return null;
}

/**
 * Datos in service.
 * 
 * @return the thread request
 */
private ThreadRequest datosInService() {
    ThreadRequest request = new ThreadRequest();
    if(context.getArguments() != null && context.getArguments().getInt("codUsuario") != 0){
        datos.setCodUSuario(context.getArguments().getInt("codUsuario"));       
        datos.setUrl(context.getArguments().getString("url"));
        request.setCodigoUsuario(datos.getCodUSuario());
        request.setUrl(datos.getUrl());

    }
    request.setFecha(datos.getFechaSeleccionada());
    request.setCodigoTipoRecurso(datos.getCodigoTipoRecurso());

    return request;
}

/**
 * Abre la ventana modal.
 * 
 * @param v
 *            the v
 */
private void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment(buttonFecha, datos);
    newFragment.show(context.getFragmentManager(), "datePicker");

}

/**
 * Acceso recursos.
 * 
 * @param request
 *            the request
 */
private void accesoRecursos(ReservaRequest request) {
    android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, new ReservaOcupacionFragment(request)).commit();
    return;
}

}

    
asked by StarApps 31.03.2017 в 12:11
source

1 answer

0

so I see these using the fragments of the support library which is fine but when you get the fragmentmanager you should get it like this:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

getActivity () because you are in a fragment and this method is for activities.

The same thing in this function would be like this:

private void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment(buttonFecha, datos);
    newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
}
    
answered by 04.04.2017 в 22:42