What's Wrong With The Second Activity Java Code?
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 thir
Solution 1:
I think your code should like this.. remove
mp = MediaPlayer.create(this, resId);
mp.start();
before switch statement
switch (v.getId()) {
case R.id.button_4:
resId = R.raw.button_4;
mp = MediaPlayer.create(this, resId);
mp.start();
break;
case R.id.button_5:
resId = R.raw.button_5;
mp = MediaPlayer.create(this, resId);
mp.start();
break;
}
Solution 2:
on your second activity, create your mediapPlayer AFTER switch case. On your way, he will try to reproduce something that still doesn't exist:
@OverridepublicvoidonCreate(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);
}
publicvoidonClick(View v) {
int resId = 1;
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;
}
// Release any resources from previous MediaPlayerif (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
UPDATE: Execute the mp.release() before any action on your onclick():
publicclassMainActivityextendsActivityimplementsOnClickListener {
private MediaPlayer mp;
@OverridepublicvoidonCreate(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);
}
publicvoidonClick(View v) {
int resId=1;
// Release any resources from previous MediaPlayerif (mp != null) {
mp.release();
}
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(newIntent(MainActivity.this,SecondActivity.class));
break;
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
Solution 3:
The error says the following:
Resources not found withid of 0x01
meaning the problem is this:
int resId = 1;
mp = MediaPlayer.create(this, resId);
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
// 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;
}
}
Are mixed up instead of
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;
}
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
Post a Comment for "What's Wrong With The Second Activity Java Code?"