RadioButtonID of AlertDialog null using restore preferences

0

I have a radiogroup built in an alertDialog. The problem is that it gives me different id to reconstruct the activity when I call the preferences file to restore the option that the user set.

findViewById(R.id._signupEridelevel).setOnClickListener((View v) ->{
    AlertDialog.Builder ridelevelbuilder = new AlertDialog.Builder(_SingUp.this);
    View vistalayout = getLayoutInflater().inflate(R.layout.build_dl_ridelevel, null);
    RadioGroup rg = vistalayout.findViewById(R.id.build_ridelevel);
    ridelevelbuilder.setView(vistalayout);
    AlertDialog dial = ridelevelbuilder.create();
    // RECUPERAR EN FICHERO DE PREFERENCIAS
// SI NO HAY PREFERENCIAS ME MARCA EL RADIOBUTTON PRINCIPAL QUE ES EL PRIMERO
    SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
    int choose = pref.getInt(PREFERENCIA_MONTA, R.id.build_ridelevel_default);
            // eliminado porque daba NULL (EL PROBLEMA) RadioButton vt = (RadioButton)vistalayout.findViewById(choose);
            // ELIMINADO(vt).setChecked(true);
    for(int x=0; x<rg.getChildCount();x++){
        int m = rg.getChildAt(x).getId();
        Log.i("RADIOGROUP",m+"");
    }
    dial.show();
    vistalayout.findViewById(R.id.build_ridelevel_accept).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // GUARDAR EN FICHERO DE PREFERENCIAS
//AQUI LO GUARDO
            SharedPreferences pref2 = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor ed = pref2.edit();
            int id  =((RadioGroup)vistalayout.findViewById(R.id.build_ridelevel)).getCheckedRadioButtonId();
            Log.i("RADIO GUARDADO",id+"");
            ed.putInt(PREFERENCIA_MONTA,((RadioGroup)vistalayout.findViewById(R.id.build_ridelevel)).getCheckedRadioButtonId());
            ed.commit();
            dial.dismiss();
        }
    });*
                    Log.i("RADIO GUARDADO",id+"");
                    ed.putInt(PREFERENCIA_MONTA,((RadioGroup)vistalayout.findViewById(R.id.build_ridelevel)).getCheckedRadioButtonId());
                    ed.commit();
                    dial.dismiss();
                }
            });

I was given a null pointer exception so what I saw gave me different id.

CONSOLA:pantalla vertical

I/RADIOGROUP: 2131296319
              3
I/RADIOGROUP: 4
              5
I/RADIOGROUP: 6
I/RADIO GUARDADO: 3
CONSOLA:pantalla horizontal
I/RADIOGROUP: 9
I/RADIOGROUP: 10
I/RADIOGROUP: 11
              12

Does anyone have a solution?

    
asked by Sara Garcia Vaquero Serrano Mo 09.05.2018 в 02:57
source

0 answers