我的Android应用在按下一组特定的Buttons
后崩溃。基本上,Activity
有两个Buttons
。一个播放声音,另一个播放另一个Activity
。但是在使用后退按钮返回并再次按下声音Button
之后,它将使应用程序崩溃。我不确定为什么Button
第一次起作用。我以为我必须重置MediaPlayer
但它仍然会崩溃。
我的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
答案 0 :(得分:1)
来自Android文档:
“IllegalStateException:内部播放器引擎尚未发生 已初始化或已被释放。“
当您通过Activity
转到新的Intent
时,之前的Activity
会通过onDestroy()
方法:
mysound.stop();
mysound.reset();
mysound.release();
用户按下后退按钮后,不会重新创建mysound
。
尝试将listen()
方法更改为:
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();
}