I am new to android and I am making an application with buttons, which reproduce sounds. I'm using Media Player, but not if I'm using it correctly. In the first activity works fine, but when I go to the second activity or even to the third and touch any button, I return an error. All activities are equal. My level of English is essential and therefore may not understand the functioning of media player well and the error is because of that, because in Castilian did not find enough information. Here is the code with the error, if they can help me with it I thank you very much.
public class Activity2 extends Activity {
private Button boton1;
private Button boton2;
private Button boton3;
private Button boton4;
private Button boton5;
private Button boton6;
private Button boton7;
private Button boton8;
private boolean start1;
private boolean start2;
private boolean start3;
private boolean start4;
private boolean start5;
private boolean start6;
MediaPlayer mp1;
MediaPlayer mp2;
MediaPlayer mp3;
MediaPlayer mp4;
MediaPlayer mp5;
MediaPlayer mp6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
boton1=(Button)findViewById(R.id.boton1);
boton2=(Button)findViewById(R.id.boton2);
boton3=(Button)findViewById(R.id.boton3);
boton4=(Button)findViewById(R.id.boton4);
boton5=(Button)findViewById(R.id.boton5);
boton6=(Button)findViewById(R.id.boton6);
boton7=(Button)findViewById(R.id.boton7);
boton8=(Button)findViewById(R.id.boton8);
mp1 = MediaPlayer.create(this, R.raw.one);
mp2 = MediaPlayer.create(this, R.raw.two);
mp3 = MediaPlayer.create(this, R.raw.three);
mp4 = MediaPlayer.create(this, R.raw.four);
mp5 = MediaPlayer.create(this, R.raw.five);
mp6 = MediaPlayer.create(this, R.raw.six);
//COLOR TITULO
if (android.os.Build.VERSION.SDK_INT >= 11){
android.app.ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#27ae60")));
}
}
public void boton1 (View v) {
if(start1==false){
mp1.start();
start1=true;
mp2.stop();
mp2.prepareAsync();
start2=false;
mp3.stop();
mp3.prepareAsync();
start3=false;
mp4.stop();
mp4.prepareAsync();
start4=false;
mp5.stop();
mp5.prepareAsync();
start5=false;
mp6.stop();
mp6.prepareAsync();
start6=false;
}
else{
mp1.stop();
mp1.prepareAsync();
start1=false;
}
}
public void boton2 (View v) {
if(start2==false){
mp2.start();
start2=true;
mp1.stop();
mp1.prepareAsync();
start1=false;
mp3.stop();
mp3.prepareAsync();
start3=false;
mp4.stop();
mp4.prepareAsync();
start4=false;
mp5.stop();
mp5.prepareAsync();
start5=false;
mp6.stop();
mp6.prepareAsync();
start6=false;
}
else{
mp2.stop();
mp2.prepareAsync();
start2=false;
}
}
//Use the same logic with other buttons (to 6)
//And this are the next button and onDestroy:
public void boton7 (View v){
mp1.stop();
mp1.prepareAsync();
start1=false;
mp2.stop();
mp2.prepareAsync();
start2=false;
mp3.stop();
mp3.prepareAsync();
start3=false;
mp4.stop();
mp4.prepareAsync();
start4=false;
mp5.stop();
mp5.prepareAsync();
start5=false;
mp6.stop();
mp6.prepareAsync();
start6=false;
Intent aSiguientePantalla = new Intent(Activity2.this, Activity3.class);
startActivity(aSiguientePantalla);
overridePendingTransition(R.anim.rightleft1, R.anim.rightleft2);
Activity2.this.finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mp1 != null) {
mp1.release();
mp1 = null;
}
if (mp2 != null) {
mp2.release();
mp2 = null;
}
if (mp3 != null) {
mp3.release();
mp3 = null;
}
if (mp4 != null) {
mp4.release();
mp4 = null;
}
if (mp5 != null) {
mp5.release();
mp5 = null;
}
if (mp6 != null) {
mp6.release();
mp6 = null;
}
}
//These are the errors
02-10 11:45:05.853: E/AndroidRuntime(13639): FATAL EXCEPTION: main
02-10 11:45:05.853: E/AndroidRuntime(13639): Process: com.example.realsounds, PID: 13639
02-10 11:45:05.853: E/AndroidRuntime(13639): java.lang.IllegalStateException: Could not execute method of the activity
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.view.View$1.onClick(View.java:3841)
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.view.View.performClick(View.java:4456)
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.view.View$PerformClick.run(View.java:18465)
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.os.Handler.handleCallback(Handler.java:733)
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.os.Handler.dispatchMessage(Handler.java:95)
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.os.Looper.loop(Looper.java:136)
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.app.ActivityThread.main(ActivityThread.java:5086)
02-10 11:45:05.853: E/AndroidRuntime(13639): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 11:45:05.853: E/AndroidRuntime(13639): at java.lang.reflect.Method.invoke(Method.java:515)
02-10 11:45:05.853: E/AndroidRuntime(13639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-10 11:45:05.853: E/AndroidRuntime(13639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-10 11:45:05.853: E/AndroidRuntime(13639): at dalvik.system.NativeStart.main(Native Method)
02-10 11:45:05.853: E/AndroidRuntime(13639): Caused by: java.lang.reflect.InvocationTargetException
02-10 11:45:05.853: E/AndroidRuntime(13639): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 11:45:05.853: E/AndroidRuntime(13639): at java.lang.reflect.Method.invoke(Method.java:515)
02-10 11:45:05.853: E/AndroidRuntime(13639): at android.view.View$1.onClick(View.java:3836)
02-10 11:45:05.853: E/AndroidRuntime(13639): ... 11 more
02-10 11:45:05.853: E/AndroidRuntime(13639): Caused by: java.lang.NullPointerException
02-10 11:45:05.853: E/AndroidRuntime(13639): at com.example.realsounds.Activity2.boton4(Activity2.java:183)
02-10 11:45:05.853: E/AndroidRuntime(13639): ... 14 more
Thanks for your time. Regards
Aucun commentaire:
Enregistrer un commentaire