0

.getPreferences me da un error de tipo NullPointer, me gustaria saber que debo agregar o quitar para que no se produzca este error

if (clicks == 17){

    SharedPreferences sharedPref = getActivity()**.getPreferences**(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("codigoSemana", 101);
    editor.apply();
    Navigation.findNavController(v).navigate(R.id.calendarioFragment);
}

1 respuesta 1

0

Agrega el método onAttach() y guarda el contexto en una variable local:

@Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
         mContext = context;
    }

Cambia getPreferences() por getSharedPreferences() para usar el contexto:

SharedPreferences preferences = mContext.getSharedPreferences("preferences",Context.MODE_PRIVATE);

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.