-3

我有一个播放声音的 android 应用程序,但是当我按 HOME/BACK 时,我希望声音停止并关闭应用程序,当用户返回应用程序时,我想再次开始活动而不是恢复。

我的 onPause() 代码:

@Override
protected void onPause()
{
    super.onPause();
    if (pS.isPlaying()) {
        pS.pause(); //stop the sound
        finish();
    }
    else
        finish();
}

当声音播放时,我点击 HOME 或 BACK 我的应用程序 FC 并在下面发布日志:

04-02 13:06:40.563: E/AndroidRuntime(925): FATAL EXCEPTION: main
04-02 13:06:40.563: E/AndroidRuntime(925): java.lang.RuntimeException: Unable to pause activity {com.testcom.myapp/com.testcom.myapp.MyappActivity}: java.lang.NullPointerException
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.access$1700(ActivityThread.java:117)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.os.Looper.loop(Looper.java:123)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 13:06:40.563: E/AndroidRuntime(925):  at java.lang.reflect.Method.invokeNative(Native Method)
04-02 13:06:40.563: E/AndroidRuntime(925):  at java.lang.reflect.Method.invoke(Method.java:507)
04-02 13:06:40.563: E/AndroidRuntime(925):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 13:06:40.563: E/AndroidRuntime(925):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 13:06:40.563: E/AndroidRuntime(925):  at dalvik.system.NativeStart.main(Native Method)
04-02 13:06:40.563: E/AndroidRuntime(925): Caused by: java.lang.NullPointerException
04-02 13:06:40.563: E/AndroidRuntime(925):  at com.testcom.myapp.MyappActivity.onPause(MyappActivity.java:119)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.Activity.performPause(Activity.java:3851)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
04-02 13:06:40.563: E/AndroidRuntime(925):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341)
04-02 13:06:40.563: E/AndroidRuntime(925):  ... 12 more

注意:如果正在播放声音并按下 HOME/BACK,则应用程序关闭正常,但如果在按下 HOME/BACK 时播放应用程序 FC 没有声音。我该如何解决这个问题?

4

2 回答 2

4

尝试:

@Override
protected void onPause()
{
    super.onPause();
    if (pS!= null && pS.isPlaying()) {
        pS.pause(); //stop the sound
        finish();
    }
    else
        finish();
}

我猜最有可能在没有声音播放时你的pS变量为空,因此在使用它时你会得到一个 NPE。

于 2013-04-02T13:22:06.747 回答
0

在您可以完成()您的活动之前,无需在活动生命周期中等待 onpause()

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{     

if(keyCode == KeyEvent.KEYCODE_HOME||keyCode == KeyEvent.KEYCODE_BACK)
{
    if (ps!=null) {
    pS.pause(); //stop the sound
    finish();
}
else
  finish();
}
});
于 2013-04-02T13:24:38.793 回答