停止播放声音时应用程序停止

时间:2013-06-24 22:53:47

标签: android eclipse eclipse-plugin android-mediaplayer

我创建了 Android 应用,以便在用户选择选项值时播放声音。一切运作良好但是当我添加我的代码以使用户静音或在他的手机上点击锁定按钮时停止声音它运作良好但是当我旋转移动< strong>水平错误“应用程序意外停止”发生。我检查了我的代码,并在 onPause 方法中找到了错误。

@Override 
protected void onResume() {

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);


    registerReceiver(new IntentListener(), intentFilter);

    super.onResume();
}


@Override
protected void onPause() {
    IntentFilter intentFilter = new IntentFilter();
    super.onPause(); // Don't forget this line
    mp.pause(); // Or whatever the function is to pause it
    unregisterReceiver(new IntentListener());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.activity);
}

<activity
        android:name="xx"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden|screenSize">

06-25 13:59:31.442: W/KeyCharacterMap(331): No keyboard for id 0
06-25 13:59:31.442: W/KeyCharacterMap(331): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-25 13:59:31.592: D/AndroidRuntime(331): Shutting down VM
06-25 13:59:31.603: W/dalvikvm(331): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-25 13:59:31.672: E/AndroidRuntime(331): FATAL EXCEPTION: main
06-25 13:59:31.672: E/AndroidRuntime(331): java.lang.RuntimeException: Unable to pause activity {com.ramadan/com.ramadan.Ramadan}: java.lang.NullPointerException
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.ActivityThread.access$2500(ActivityThread.java:125)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.os.Looper.loop(Looper.java:123)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-25 13:59:31.672: E/AndroidRuntime(331):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 13:59:31.672: E/AndroidRuntime(331):  at java.lang.reflect.Method.invoke(Method.java:521)
06-25 13:59:31.672: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-25 13:59:31.672: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-25 13:59:31.672: E/AndroidRuntime(331):  at dalvik.system.NativeStart.main(Native Method)
06-25 13:59:31.672: E/AndroidRuntime(331): Caused by: java.lang.NullPointerException
06-25 13:59:31.672: E/AndroidRuntime(331):  at     com.ramadan.Ramadan.onPause(Ramadan.java:125)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.Activity.performPause(Activity.java:3842)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190)
06-25 13:59:31.672: E/AndroidRuntime(331):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3335)
06-25 13:59:31.672: E/AndroidRuntime(331):  ... 12 more

2 个答案:

答案 0 :(得分:3)

如果您查看崩溃的logcat输出,您将获得更多信息,因为它将为您提供异常的原因。但是,仅查看上面的代码,您无法使用不同的实例调用registerReceiver()unregisterReceiver(),它们必须是同一个对象。对unregisterReceiver()的调用很可能是一个例外,说明之前从未注册过接收者实例。您的代码需要看起来更像这样:

//We need a reference to this instance somehow
private IntentListener listener = new IntentListener();

@Override
public void onResume() {

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);

    registerReceiver(listener, intentFilter);

    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(listener); //must be the same instance you registered!
}

答案 1 :(得分:0)

很难在没有logcat的情况下调查问题

但是,您可以通过在Manifest中添加配置更改来跳过重新启动旋转

    <activity
        android:name="com.youclassname.xxx"
        android:configChanges="orientation|keyboardHidden|screenSize"/>