簡體   English   中英

應用程式在關閉活動后關閉

[英]app closes after closing an activity

每次在打開第二個活動后測試應用程序,然后在手機上按“后退”按鈕,應用程序“不幸地停止”。 我在Google上找不到任何可以解釋的信息! 請幫助! 這是我的第一個應用程序,我的技能也不是很好!

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)

這是因為您在onPause()釋放了MediaPlayer
當您啟動另一個活動時,當前活動中的onPause()將被調用,因此您的Activity會釋放您在onCreate()創建的mysoundmysound2mysound3
您必須在onResume()重新創建它們。

有關更多信息,請參見活動生命周期

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM