RadioGroup null RadioButton in alertdialog in restore preferences

0

I would like please if you can help me know what happens. 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 null pointer exception so what I saw gave me different id. CONSOLE: vertical screen

I / RADIOGROUP: 2131296319               3 I / RADIOGROUP: 4               5 I / RADIOGROUP: 6 I / RADIO SAVED: 3 CONSOLE: horizontal screen I / RADIOGROUP: 9 I / RADIOGROUP: 10 I / RADIOGROUP: 11               12

Does anyone have a solution? Thanks

    
asked by Sara Garcia Vaquero Serrano Mo 09.05.2018 в 04:32
source

0 answers