My Android app crashes after a specific set of Buttons
are pressed. Basically, the Activity
has two Buttons
. One plays a sound and the other opens another Activity
. But after going back with the back button and pressing the sound Button
again, it will crash the app. I'm not sure why since the Button
works the first time. I thought I had to reset the MediaPlayer
but it still crashes.
My Activity
:
public class Tables1 extends Activity {
MediaPlayer mysound;
protected boolean active = true;
protected int splashtime = 17000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twotime1);
mysound = MediaPlayer.create(Tables1.this, R.raw.two1);
}
@Override
protected void onDestroy() {
super.onDestroy();
mysound.stop();
mysound.reset();
mysound.release();
mysound = null;
}
public void listen(View view) {
mysound.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mysound) {
mysound.release();
}
});
mysound.start();
}
public void Next(View view) {
if (mysound.isPlaying()) {
mysound.stop();
mysound.reset();
} else {
Log.d("Exercise", "not working ");
}
Intent i = new Intent();
i.setClassName("com.example", "com.example.twotimes.Exercise1");
startActivity(i);
}
public void back(View view) {
if (mysound.isPlaying()) {
mysound.stop();
}
Intent i = new Intent();
i.setClassName("com.example", "com.example.timestableseasy.Menu2");
startActivity(i);
}
}
Logcat:
05-30 15:00:35.954: E/AndroidRuntime(31572): FATAL EXCEPTION: main
05-30 15:00:35.954: E/AndroidRuntime(31572): java.lang.IllegalStateException: Could not execute method of the activity
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.view.View$1.onClick(View.java:3814)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.view.View.performClick(View.java:4421)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.view.View$PerformClick.run(View.java:17903)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.os.Handler.handleCallback(Handler.java:730)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.os.Looper.loop(Looper.java:213)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.app.ActivityThread.main(ActivityThread.java:5225)
05-30 15:00:35.954: E/AndroidRuntime(31572): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 15:00:35.954: E/AndroidRuntime(31572): at java.lang.reflect.Method.invoke(Method.java:525)
05-30 15:00:35.954: E/AndroidRuntime(31572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
05-30 15:00:35.954: E/AndroidRuntime(31572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-30 15:00:35.954: E/AndroidRuntime(31572): at dalvik.system.NativeStart.main(Native Method)
05-30 15:00:35.954: E/AndroidRuntime(31572): Caused by: java.lang.reflect.InvocationTargetException
05-30 15:00:35.954: E/AndroidRuntime(31572): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 15:00:35.954: E/AndroidRuntime(31572): at java.lang.reflect.Method.invoke(Method.java:525)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.view.View$1.onClick(View.java:3809)
05-30 15:00:35.954: E/AndroidRuntime(31572): ... 11 more
05-30 15:00:35.954: E/AndroidRuntime(31572): Caused by: java.lang.IllegalStateException
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.media.MediaPlayer._start(Native Method)
05-30 15:00:35.954: E/AndroidRuntime(31572): at android.media.MediaPlayer.start(MediaPlayer.java:1082)
05-30 15:00:35.954: E/AndroidRuntime(31572): at com.example.twotimes.Tables1.listen(Tables1.java:52)
05-30 15:00:35.954: E/AndroidRuntime(31572): ... 14 more
From the Android Documentation:
"IllegalStateException: the internal player engine has not been initialized or has been released."
When you go to your new Activity
via an Intent
, your previous Activity
goes through your onDestroy()
method:
mysound.stop();
mysound.reset();
mysound.release();
your mysound
is not re-created once the user presses the back button.
Try changing your listen()
method to this:
public void listen(View view) {
mysound = MediaPlayer.create(Tables1.this, R.raw.two1);
mysound.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mysound) {
mysound.release();
}
});
mysound.start();
}