1

Immediate Disclaimer: I am not a programmer, I've been dumped with this as part of a group project, so apologies if the code is shabby.

I've got a main activity as the start-up page with several buttons that should open different activities, three of these buttons work perfectly with opening up their specific activities (Main2Activity, MOT and Garage), but the others, with the same structure being used, just close the app instead of opening the next screen.

public void defineButtons() {
        findViewById(R.id.mot_button).setOnClickListener(buttonClickListener);
        findViewById(R.id.enter_button).setOnClickListener(buttonClickListener);
        findViewById(R.id.garage_button).setOnClickListener(buttonClickListener);
        findViewById(R.id.profile_button).setOnClickListener(buttonClickListener);
        findViewById(R.id.contact_button).setOnClickListener(buttonClickListener);
        findViewById(R.id.settings_button).setOnClickListener(buttonClickListener);
    }

private View.OnClickListener buttonClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mot_button:
                Intent intent = new Intent(MainActivity.this, MOT.class);
                startActivity(intent);
                break;
            case R.id.garage_button:
                Intent x = new Intent(MainActivity.this, garage.class);
                startActivity(x);
                break;
            case R.id.profile_button:
                Intent a = new Intent(MainActivity.this, Profile.class);
                startActivity(a);
                break;
            case R.id.contact_button:
                Intent b = new Intent(MainActivity.this, Contact.class);
                startActivity(b);
                break;
            case R.id.settings_button:
                Intent c = new Intent(MainActivity.this, Activity_Settings.class);
                startActivity(c);
                break;
            case R.id.enter_button:
                reg_input=findViewById(R.id.reg_input);
                Intent i = new Intent(MainActivity.this, Main2Activity.class);
                regNo = reg_input.getText().toString();
                i.putExtra("Value", regNo);
                startActivity(i);
                finish();
                break;

This is the relevant code for it, let me know if you want to see anything else. I'm probably being really stupid, but I'd appreciate the help.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
JayPeg
  • 13
  • 1
  • 4
  • If by "closes" you mean that your app is crashing, use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare May 08 '20 at 14:57
  • Your app is crashing, probably because the activities your want to start aren't in the manifest. Post the content of your Android manifest in your question. – David Wasser May 08 '20 at 16:03
  • 1
    @DavidWasser, I never even thought to look at the manifest, instantly fixed, thank you – JayPeg May 08 '20 at 22:48
  • Great that you've solved your problem! I created an answer, you can accept it by clicking the green checkmark. This will remove the question from the list of unanaswered questions and maybe help others who have a similar problem. – David Wasser May 09 '20 at 10:25

1 Answers1

0

Your app is crashing, probably because the activities your want to start aren't in the manifest. Check your manifest and make sure that all your activities are declared there.

David Wasser
  • 93,459
  • 16
  • 209
  • 274