1

i have an activity with navigation drawer with fragments and its compiling with androidx, in one of them i want to call a fragment that contains a custom caldroid and until now it works fine, but at the moment to finish the fragment or change to another fragment the logcat call this error:

I searched on internet and found a similar case, https://issuetracker.google.com/issues/135764330 and it is only related to projects that work with androidx, but i don't know how to apply the changes in the library to eliminate this error.

Error Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: edu.usil.staffmovil, PID: 29749
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()' on a null object reference
        at androidx.fragment.app.Fragment.performDetach(Fragment.java:2849)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1034)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
        at androidx.fragment.app.FragmentManagerImpl.dispatchDestroy(FragmentManagerImpl.java:2644)
        at androidx.fragment.app.Fragment.performDestroy(Fragment.java:2825)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1028)
        at androidx.fragment.app.FragmentManagerImpl$3$1.run(FragmentManagerImpl.java:1099)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Code from the fragment:

public class EventHomeFragment extends Fragment {

public static final String TAG = "EventHomeFragment";
private int diaSemana=0, diaMes=0, mes=0, anio=0, diaSelect=0;
    private CaldroidFragment caldroid;
    private int altoContenido=0, altoFila=0, posicionScroll=0, altoMesAnio = 0, altoSemana = 0, mesVisible = 0, anioVisible = 0;
    private Calendar fechaSelect;
//More Definitions
//...
//...

public EventHomeFragment() {
        // Required empty public constructor
    }



@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        fechaSelect = Calendar.getInstance();

        Calendar cal = Calendar.getInstance();
        diaSemana = cal.get(Calendar.DAY_OF_WEEK);
        diaMes = cal.get(Calendar.DAY_OF_MONTH);
        mes = cal.get(Calendar.MONTH);
        anio = cal.get(Calendar.YEAR);

        mesVisible = mes;
        anioVisible = anio;
setViewsComponentsAndEvents();
            fechaSelect.setTime(FechaUtils.getDateFromStringYYYYMMMDD( anio + "-" + ((mes + 1) < 10 ? ("0" + mes) : mes) + "-" + diaMes));
        }

private void setViewsComponentsAndEvents() {

    setDatosSemanaCabecera();
    setListarEventosPantalla();
    setCustomCaldroid();
    manejador_sliding.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!expand) {
                expand();
            } else {
                collapse();
            }
        }
    });

    lista_eventos.post(new Runnable() {
        @Override
        public void run() {
            //Log.e("post","entramos");
            configurarViewsEventos();
        }
    });

}

private void setCustomCaldroid() {

    CaldroidListener listener = new CaldroidListener() {

        @Override
        public void onSelectDate(Date date, View view) {
            long currTime = System.currentTimeMillis();
            if(timeLastClick!=0){
                if(currTime - timeLastClick < ViewConfiguration.getDoubleTapTimeout()){
                    return;
                }
            }else{
                timeLastClick = currTime;
            }

            Calendar fecha = Calendar.getInstance();
            fecha.setTime(date);
            setFechaSelectedFromCalendar(fecha.get(Calendar.DAY_OF_WEEK), fecha.get(Calendar.DAY_OF_MONTH), fecha.get(Calendar.MONTH), fecha.get(Calendar.YEAR));

            if(lastSelected!=null){
                Calendar lastSelectedFecha = Calendar.getInstance();
                lastSelectedFecha.setTime(lastSelected);
                if(fecha.get(Calendar.DAY_OF_MONTH)==lastSelectedFecha.get(Calendar.DAY_OF_MONTH) &&
                        fecha.get(Calendar.MONTH)==lastSelectedFecha.get(Calendar.MONTH) &&
                        fecha.get(Calendar.YEAR)==lastSelectedFecha.get(Calendar.YEAR)){
                    mismaFecha=true;
                }else{
                    mismaFecha=false;
                }
                caldroid.clearBackgroundDrawableForDate(lastSelected);
            }else{
                mismaFecha = true;
            }
            //si el mes que se muestra es mayor que el mes de la fecha seleccionada
            //entonces se ha seleccionado la una fecha del mes anterior
            //se esta en la primera fila
            if(anio==anioVisible) {
                if (mesVisible > mes) {
                    posicionScroll = 0;
                }
                if (mesVisible < mes) {
                    Calendar fechaPrimero = Calendar.getInstance();
                    fechaPrimero.set(anioVisible, mesVisible, 1);
                    fechaPrimero.setMinimalDaysInFirstWeek(1);
                    int semanaMes = fechaPrimero.getActualMaximum(Calendar.WEEK_OF_MONTH);
                    posicionScroll = altoFila * (semanaMes - 1);
                }
                if(mesVisible==mes){
                    int semanaMes = fechaSelect.get(Calendar.WEEK_OF_MONTH);
                    posicionScroll = 0;
                    if(semanaMes>1){
                        posicionScroll = altoFila*(semanaMes-1);
                    }
                }
            }

            if(anioVisible>anio){
                posicionScroll = 0;
            }

            if(anioVisible<anio){
                Calendar fechaPrimero = Calendar.getInstance();
                fechaPrimero.set(anioVisible, mesVisible, 1);
                fechaPrimero.setMinimalDaysInFirstWeek(1);
                int semanaMes = fechaPrimero.getActualMaximum(Calendar.WEEK_OF_MONTH);
                posicionScroll = altoFila * (semanaMes - 1);
            }

            lastSelected = date;
//                caldroid.setBackgroundResourceForDate(R.color.color_evento_selected, lastSelected);
            caldroid.refreshView();

            moveScrollToDateSelected();

            if(expand && !mostrarMensaje && moveScrollToDateSelected()){
                collapse();
            }

        }
    };

    caldroid = new CaldroidFragment();

    Bundle args = new Bundle();
    Calendar fecha = Calendar.getInstance();
    fecha.setTime(FechaUtils.getDateFromStringYYYYMMMDD(anio + "-" + (mes + 1) + "-" + diaMes));
    args.putInt(CaldroidFragment.MONTH, fecha.get(Calendar.MONTH) + 1);
    args.putInt(CaldroidFragment.YEAR, fecha.get(Calendar.YEAR));
    args.putInt(CaldroidFragment.THEME_RESOURCE, R.style.CaldroidEventosUsil);
    args.putInt(CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY);
    args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, false);

    caldroid.setBackgroundDrawableForDate(getResources().getDrawable(R.color.color_evento_selected), fechaSelect.getTime());
    caldroid.setArguments(args);
    trans = getChildFragmentManager().beginTransaction();
    trans.addToBackStack(null);
    trans.replace(R.id.fragment_custom_caldroid, caldroid);
//        trans.remove();
    trans.commit();


}


}

1 Answer 1

4

By using an updated version Caldroid resolved my problem. Try using https://github.com/vpotvin/CaldroidX created by @roomorama

this library compatible with newer versions of Android fragments and AndroidX.

only replacing the Caldroid dependency resolved my issue. change dependency in Gradle file to below

  implementation 'com.github.vpotvin:caldroidx:1.0'
Sign up to request clarification or add additional context in comments.

2 Comments

Nice ! in addition :
In your project level build.gradle: allprojects { repositories { ... maven { url 'jitpack.io' } } } In your app level build.gradle dependencies { implementation 'com.github.vpotvin:caldroidx:1.0' }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.