19

I'm trying to move onto the third activity in a sequence. Going from the main activity to the second one works fine but when I try to go to the third activity from the second I the application crashes.

Here's my code for the second activity:

package com.example.helloandroid;

import android.app.Activity;
//other imports here

public class Game extends Activity implements OnClickListener {

    private static final String TAG = "Matrix";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.matrix);
        View doneButton = findViewById(R.id.done_button);
        doneButton.setOnClickListener(this);
    }

    public void onClick(View v) { 
        switch (v.getId()) { 
            case R.id.done_button:
                Intent k = new Intent(this, GameTwo.class);
                startActivity(k);
                //finish();
                break;
        }
    }
}

And the code for the third activity:

package com.example.helloandroid;

import android.app.Activity;
//other imports here

public class GameTwo extends Activity {

   private static final String TAG = "Matrix";

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.setContentView(R.layout.matrixtwo);
       View donetwoButton = findViewById(R.id.donetwo_button);
   }
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Biggsy
  • 451
  • 4
  • 9
  • 20
  • 1
    Can you paste the Exception here? Use ADB LOGCAT – Swaroop Mar 02 '11 at 04:46
  • Not sure but try taking Button in second activity instead of View and then read view((Button)findViewById()) in button's object. That might solve your problem or else as @swaroop said paste exception up here so that other can understand. – Ankit Mar 02 '11 at 05:19
  • http://stackoverflow.com/questions/13194081/how-to-open-a-second-activity-on-click-of-button-in-android-app – Ali Gh Jun 11 '15 at 05:26

6 Answers6

24

Try the following code in the switch:

try {
    Intent k = new Intent(Game.this, GameTwo.class);
    startActivity(k);
} catch(Exception e) {
    e.printStackTrace();
}

Tell me is this helpful.....

Noman
  • 887
  • 1
  • 15
  • 34
Sumant
  • 2,775
  • 2
  • 22
  • 30
  • Not necessarily needed but it helps while developing. – Sam Feb 23 '16 at 05:46
  • 3
    Catching an Exception and swallowing it without showing any signal is a very bad practice... If you don't want to let it to go up the methods' chain, at least log its message or its stack trace in the console or (better) in a file. – Massimiliano Kraus Mar 16 '17 at 08:50
11
Intent k = new Intent(Game.this, GameTwo.class);
startActivity(k);

This works, but you also want to make sure that you specify this in your manifest.

Noman
  • 887
  • 1
  • 15
  • 34
4

Be sure to have the three Activities declared in the manifest. Is a common error to create the Activity and not declare it.

Call the new Activity using:

Intent k = new Intent(Game.this, GameTwo.class);
startActivity(k);
EhTd
  • 3,260
  • 4
  • 19
  • 18
4

Try this

Intent intent = new Intent(getApplicationContext(), GameTwo.class);
startActivity(intent);
Joe
  • 246
  • 1
  • 3
  • 15
1

Make sure the activity is declared in the manifest.

If you just want to move from one activity to another, you can follow the below code.

startActivity(new Intent(getApplicationContext(), NextActivity.class));

If you want to send any data while moving to another activity, then follow the code below.

Intent intent = new Intent(MainActivity.this, NextActivity.class);
        intent.putExtra("Title", title);
        startActivity(intent);
0

It's a long shot but...
Your problem could also be caused by a NullPointerException
which is thrown if donetwo_button isn't declared in matrixtwo.xml...
(Copy-paste errors are quite common)

Bojan Radivojevic
  • 717
  • 3
  • 12
  • 23