I am trying to design the app that has 3 buttons on the main screen; when the user presses on the first two buttons it plays the different music located in the raw folder. The third button on the main screen should drive the user to the next screen which also has 2 buttons.
I tried to run my app on the emulator. It plays the music on first two buttons of the main screen, and when I click on the third (Next) button it takes the user to the next screen. However, when I click on 1st or 2nd button of the Second Screen. It says "Unfortunately your app has stopped". I don't now what's wrong with my SecondActivity.Java code .
Any help will be greatly appreciated. Below is my MAIN and Second class JAVA code. I did declare my second Activity in manifest.xml file.
Main Activity Java Code..
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
findViewById(R.id.button_3).setOnClickListener(this);
}
public void onClick(View v) {
int resId=1;
switch (v.getId()) {
case R.id.button_1: resId = R.raw.button_1; break;
case R.id.button_2: resId = R.raw.button_2; break;
case R.id.button_3:
startActivity(new Intent(MainActivity.this,SecondActivity.class));
return;
}
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
SecondActivity.Java
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class SecondActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button_4).setOnClickListener(this);
findViewById(R.id.button_5).setOnClickListener(this);
}
public void onClick(View v) {
int resId = 1;
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
switch (v.getId()) {
case R.id.button_4: resId = R.raw.button_4; break;
case R.id.button_5: resId = R.raw.button_5; break;
}
}
}
The error says the following:
meaning the problem is this:
That is a resource ID that does not exist.
If you have these files in your /res/raw folder, then you should use the
R.raw.xyz
ID.Although I guess the problem really is just that the
Are mixed up instead of
I think your code should like this.. remove
on your second activity, create your mediapPlayer AFTER switch case. On your way, he will try to reproduce something that still doesn't exist:
UPDATE: Execute the mp.release() before any action on your onclick():