dimanche 3 juin 2018

app crashes when pressing on checkbox

I am trying to display the answers from Radio Buttons, Edit Texts & Checkboxes in one Toast. Everything is ok, until I press a Checkbox & then Submit button. At that point the app is crashing.

I tried several ways found online, but none applies to this code. Also, can I use the ArrayList here ?

Do you have any suggestions ?

public class MainActivity extends AppCompatActivity {
    EditText nombre;
    EditText pais;

    RadioGroup pi;
    RadioButton dos;

    RadioGroup mandela;
    RadioButton tres;

    EditText planetas;

    ArrayList<String> europeos = new ArrayList<String>();

    CheckBox Alemania;
    CheckBox Romania;
    CheckBox Portugal;
    CheckBox Gibraltar;

    RadioGroup joy;
    RadioButton felicidad;

    Button displayAnswers;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText nombre = (EditText) findViewById(R.id.name);
        final EditText pais = (EditText) findViewById(R.id.australia);

        final RadioGroup pi = (RadioGroup) findViewById(R.id.piRadio);

        final RadioGroup mandela = (RadioGroup) findViewById(R.id.mandelaRadio);
        RadioButton tresUno =(RadioButton) findViewById(R.id.three_one);
        RadioButton tresDos =(RadioButton) findViewById(R.id.three_two);
        RadioButton tresTres =(RadioButton) findViewById(R.id.three_three);

        final EditText planetas = (EditText) findViewById(R.id.planets);


        CheckBox Alemanha = (CheckBox) findViewById(R.id.germany);
        CheckBox Romania = (CheckBox) findViewById(R.id.romania);
        CheckBox Portugal = (CheckBox) findViewById(R.id.portugal);
        CheckBox Gibraltar = (CheckBox) findViewById(R.id.gibraltar);

        final RadioGroup joy = (RadioGroup) findViewById(R.id.joyRadio);
        RadioButton si =(RadioButton) findViewById(R.id.yes);
        RadioButton no =(RadioButton) findViewById(R.id.no);


        final Button displayAnswers = (Button) findViewById(R.id.submit);



        displayAnswers.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                final String name = nombre.getText().toString();
                final String australia = pais.getText().toString();

                final int secondQuestion = pi.getCheckedRadioButtonId();
                RadioButton dos = (RadioButton)findViewById(secondQuestion);
                String two = dos.getText().toString();

                final int thirdQuestion = mandela.getCheckedRadioButtonId();
                RadioButton tres = (RadioButton)findViewById(thirdQuestion);
                String three = tres.getText().toString();

                final String planets = planetas.getText().toString();

                String checkedCountries = "";



                final int lastQuestion = joy.getCheckedRadioButtonId();
                RadioButton felicidad = (RadioButton)findViewById(lastQuestion);
                String six = felicidad.getText().toString();

                CharSequence finaltext = "Name : " + name + "\n Anwswer to question one: " + australia + "\n Answer to question two: " + two + "\n Answer to question three: " + three + "\n Answer to question four: " + planets + "\n Answer to question five: " + checkedCountries + "\n Answer to question six: " + six;

                Toast.makeText(MainActivity.this,finaltext,200000).show();

            }
        });




    }



    public void selectButton(View view) {

        boolean checkedCountries = ((CheckBox) view).isChecked();

        switch (view.getId()) {
            case R.id.germany:
                if (checkedCountries)
                {europeos.add("Germany");}
                else
                {europeos.remove("Germany");}

                break;

            case R.id.portugal:
                if (checkedCountries)
                {europeos.add("Portugal");}

                else
                {europeos.remove("Portugal");}

                break;

            case R.id.romania:
                if (checkedCountries)
                {europeos.add("Romania");}

                else
                {europeos.remove("Romania");}

                break;

            case R.id.gibraltar:
                if (checkedCountries)
                {europeos.add("Gibraltar");}

                else
                {europeos.remove("Gibraltar");}

                break;

        }

    }

    public void radiobtn(View view) {
        // Is the button now checked?
        boolean checkedPi = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.two_one:
                if (checkedPi)
                    // Show False
                    break;
            case R.id.two_two:
                if (checkedPi)
                    // Show True
                    break;
            case R.id.two_three:
                if (checkedPi)
                    // Show False
                    break;
        }
    }

    public void radiobtn2(View view) {
        // Is the button now checked?
        boolean checkedMandela = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.three_one:
                if (checkedMandela)
                    // Show False
                    break;
            case R.id.three_two:
                if (checkedMandela)
                    // Show True
                    break;
            case R.id.three_three:
                if (checkedMandela)
                    // Show False
                    break;
        }
    }

    public void radiobtn3(View view) {
        // Is the button now checked?
        boolean checkedJoy = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.yes:
                if (checkedJoy)
                    // Show Yes
                    break;
            case R.id.no:
                if (checkedJoy)
                    // Show No
                    break;
        }
    }

}

Thank you in advance for your help.




Aucun commentaire:

Enregistrer un commentaire