0

So am a fledgling in app development and am stuck on my first project where I want the user to be taken to a different activity which will display different results depending on the radio button the user has selected. After some research I came up with the code below (where I think the problem is). The App starts with no problem at the main activity. However when I click the button to take me to the second activity which contains the radio group, the app crashes and quits. what could be the problem? Kindly assist.

public class DestinationActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_destination); // ----- NEW CODE STARTS ON THE LINE BELOW
    final RadioButton eastlands =  findViewById(R.id.eastlands);
    final RadioButton westlands =  findViewById(R.id.westlands);
    final RadioButton thika_rd =  findViewById(R.id.thika_rd);
    final RadioButton industrial_area =  findViewById(R.id.industrial_area);
    final RadioButton cbd =  findViewById(R.id.cbd);
    final RadioButton kiambu =  findViewById(R.id.kiambu);
    final RadioButton juja =  findViewById(R.id.juja);
    final RadioButton makongeni =  findViewById(R.id.makongeni);
    final RadioButton utawala =  findViewById(R.id.utawala);
    this.finish();



final Button go =  findViewById(R.id.button_start);
    go.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (eastlands.isChecked()) {
            Intent Intents= new Intent(DestinationActivity.this, ResultActivity.class); // <----- START "EASTLANDS/RESULT" ACTIVITY
            startActivity(Intents);  // <-------- INTENTS NAMED ALPHABETICALLY a, b, c, d........
            setContentView(R.layout.activity_result);
        }
        else if (westlands.isChecked()) {
            Intent Intenta = new Intent(getApplicationContext(), WestlandsActivity.class); // <----- START "WESTLANDS" ACTIVITY
            startActivityForResult(Intenta, 0);
        }
        else if (thika_rd.isChecked()) {
            Intent Intentb = new Intent(getApplicationContext(), ThikaActivity.class); // <----- START "THIKA" ACTIVITY
            startActivityForResult(Intentb, 0);
        }
        else if (industrial_area.isChecked()) {
            Intent Intentc = new Intent(getApplicationContext(), IndustrialActivity.class); // <----- START "INDUSTRIAL" ACTIVITY
            startActivityForResult(Intentc, 0);
        }
        else if (cbd.isChecked()) {
            Intent Intentc = new Intent(getApplicationContext(), CbdActivity.class); // <----- START "CBD" ACTIVITY
            startActivityForResult(Intentc, 0);
        }
        else if (kiambu.isChecked()) {
            Intent Intentd = new Intent(getApplicationContext(), KiambuActivity.class); // <----- START "KIAMBU" ACTIVITY
            startActivityForResult(Intentd, 0);
        }
        else if (juja.isChecked()) {
            Intent Intente = new Intent(getApplicationContext(), JujaActivity.class); // <----- START "JUJA" ACTIVITY
            startActivityForResult(Intente, 0);
        }
        else if (makongeni.isChecked()) {
            Intent Intentf = new Intent(getApplicationContext(), MakongeniActivity.class); // <----- START "MAKONGENI" ACTIVITY
            startActivityForResult(Intentf, 0);
        }
        else if (utawala.isChecked()) {
            Intent Intentg = new Intent(getApplicationContext(), UtawalaActivity.class); // <----- START "UTAWALA" ACTIVITY
            startActivityForResult(Intentg, 0);
        }
    }
});

}

joeybab3
  • 295
  • 2
  • 7
  • 24

2 Answers2

4

You should delete the line with this.finish(). Cause finish() method quits the activity.

Nurbol
  • 371
  • 2
  • 8
0

You should move finish() this line to inside of onClick and place in last line into it.