每次我打开第二个活动后测试我的应用程序,然后按手机上的后退按钮,应用程序“不幸停止”。我无法在谷歌上找到任何解释的东西!请帮助!它是我的第一个应用程序,我的技能不是很好!
MediaPlayer mySound;
MediaPlayer mySound2;
MediaPlayer mySound3;
@Override
protected void onPause() {
super.onPause();
mySound.release();
mySound2.release();
mySound3.release();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound = MediaPlayer.create(this, R.raw.mlg);
mySound2 = MediaPlayer.create(this, R.raw.damnson);
mySound3 = MediaPlayer.create(this, R.raw.noscoped);
}
public void playMusic(View view) {
mySound.start();
}
public void playMusic2(View view) {
mySound2.start();
}
public void playMusic3(View view) {
mySound3.start();
}
public void buttonOnClick(View v) {
Button button=(Button) v;
startActivity(new Intent(getApplicationContext(), MainActivity2.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Logcat错误跟踪:
01-25 21:41:41.959 21891-21891/com.xlbeast.awsomemlg E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xlbeast.awsomemlg, PID: 21891
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3969)
at android.view.View.performClick(View.java:4633)
at android.widget.CompoundButton.performClick(CompoundButton.java:104)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3964)
at android.view.View.performClick(View.java:4633)
at android.widget.CompoundButton.performClick(CompoundButton.java:104)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)
at android.media.MediaPlayer.start(MediaPlayer.java:1342)
at com.xlbeast.awsomemlg.MainActivity.playMusic3(MainActivity.java:48)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3964)
at android.view.View.performClick(View.java:4633)
at android.widget.CompoundButton.performClick(CompoundButton.java:104)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这是因为您在MediaPlayer
中发布了onPause()
当您开始其他活动时,系统会调用当前活动中的onPause()
,因此您的活动会释放您在{{1}中创建的mysound
,mysound2
和mysound3
}。
您必须在onCreate()
。
有关详细信息,请参阅Activity life cycle。