0

I've studied from another app I coded in class which uses the Intent to change to another class and layout and I thought the code was similar to what I have below. As it is, when I tap the 'New Game' button it simply crashes. I played around with commenting out the .stop and .release functions of my MediaPlayer and I did notice it kills the music before it tries to get into the Intent (then crashes). I'd appreciate any help, I've spent a few days trying to dig through forums :)

MainActivity.java:

public class MainActivity extends Activity {

private MediaPlayer mpTheme;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mpTheme=MediaPlayer.create(MainActivity.this, R.raw.sttngtheme);
    mpTheme.start();
    Button newGameButton = (Button) findViewById(R.id.newGameButton);
    Button loadGameButton = (Button) findViewById(R.id.loadGameButton);
    Button instructionsButton = (Button) findViewById(R.id.instructionsButton);
    final MainActivity mainActivity = this;

    newGameButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mpTheme.stop();
            mpTheme.release();
            //Toast.makeText(getBaseContext(), "You clicked New Game", Toast.LENGTH_LONG).show();
            Intent startGameIntent = new Intent(mainActivity, Game.class);
            startActivity(startGameIntent);

        }
    });

    loadGameButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getBaseContext(), "Loading capability not yet coded.", Toast.LENGTH_LONG).show();

        }
    });


    instructionsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(), "Instructions not yet coded.", Toast.LENGTH_LONG).show();

        }
    });


}//end onCreate

@Override
protected void onPause(){
    super.onPause();
    if(mpTheme.isPlaying()){
        mpTheme.pause();
    }
}

@Override
protected void onStop(){
    super.onStop();
    if(mpTheme.isPlaying()){
        mpTheme.stop();
        mpTheme.release();
    }
}

@Override
protected void onResume(){
    super.onResume();
    if(mpTheme.isPlaying()==false){
        mpTheme.start();
    }
}

}

Game.java:

public class Game extends Activity{

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quadrantspace);
}

}

Logcat:

12-10 21:09:19.978: D/AndroidRuntime(9184): Shutting down VM

12-10 21:09:19.980: E/AndroidRuntime(9184): FATAL EXCEPTION: main

12-10 21:09:19.980: E/AndroidRuntime(9184): Process: com.ferriss.superstartrek, PID: 9184

12-10 21:09:19.980: E/AndroidRuntime(9184): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.ferriss.superstartrek/com.ferriss.superstartrek.Game}; have you declared this activity in your AndroidManifest.xml?

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1868)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1568)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.app.Activity.startActivityForResult(Activity.java:3755)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.app.Activity.startActivityForResult(Activity.java:3716)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.app.Activity.startActivity(Activity.java:4036)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.app.Activity.startActivity(Activity.java:3998)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at com.ferriss.superstartrek.MainActivity$1.onClick(MainActivity.java:34)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.view.View.performClick(View.java:4785)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.view.View$PerformClick.run(View.java:19884)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.os.Handler.handleCallback(Handler.java:746)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.os.Handler.dispatchMessage(Handler.java:95)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.os.Looper.loop(Looper.java:135)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at android.app.ActivityThread.main(ActivityThread.java:5356)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at java.lang.reflect.Method.invoke(Native Method)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at java.lang.reflect.Method.invoke(Method.java:372)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)

12-10 21:09:19.980: E/AndroidRuntime(9184):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

12-10 21:09:21.509: I/Process(9184): Sending signal. PID: 9184 SIG: 9
Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
Nick
  • 13
  • 4
  • You should post the stacktrace from the crash log. You should also see this post on how to do some basic debugging http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – codeMagic Dec 11 '16 at 03:14
  • Thanks I'll look at that post now – Nick Dec 11 '16 at 03:21
  • It appears the 4th entry is the exception I should be looking into. The 'ActivityNotFoundException' and mentions the manifest file. Looking at manifest file and the game.java file isn't mentioned there but I was told not to mess with this file? – Nick Dec 11 '16 at 03:26
  • who told you that? All of your classes that extend `Activity` need to be declared in your manifest. – trooper Dec 11 '16 at 03:35
  • My instructor for mobile apps 1. Could have been because we were brand new to it all, in the course of mobile apps 1 thru 3 we've never touched it. I'll do that and try it. Thank you – Nick Dec 11 '16 at 03:47
  • Ok, I've done all this. It still gives me an error that the app crashes. However now it does go to the next layout lol. – Nick Dec 11 '16 at 03:55

1 Answers1

1

Add your destination activity in the manifest file. For more info on how to add your Game.class to your Manifest check the official docs

Also, you don't need to declare mainActivity variable separately to use inside your newGameButton setOnClickListener method. Simply you can create an Intent like this -

Intent i = new Intent(MainActivity.this, Game.class);
startActivity(i);
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
Prasheel
  • 985
  • 5
  • 22