【问题标题】:the application stopped when stop playing the sound停止播放声音时应用程序停止
【发布时间】:2013-06-24 22:53:46
【问题描述】:

我创建了我的 Android 应用,以便在用户 选择选项值 时播放声音,我的代码做得很好。一切正常,但是当我添加我的代码以使用户在他的手机上单击 锁定按钮 时静音或停止声音时效果很好,但是当我 旋转 手机时 horizo​​ntal 发生错误“应用程序意外停止”。我检查了我的代码,并在 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

【问题讨论】:

  • 好的,通过 logcat 输出,我们可以看到在源文件 Ramadan.java 的第 125 行,在 onPause() 方法中,您正试图访问一个为空的对象。这可能是访问您的 mp 实例的行吗?当你试图访问它时表明该对象不存在。
  • 我将它更改为正确的媒体播放器实例,但是当我评论 ondestroy 方法时,没有任何问题,但声音仍然有效

标签: android eclipse eclipse-plugin android-mediaplayer


【解决方案1】:

如果您查看崩溃的 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!
}

【讨论】:

  • 它解决了问题,但声音仍然播放,直到我锁定我使用的手机 mp.release();但同样的错误又发生了
  • 那么是时候发布你的 logcat 输出了,因为它可能不是“相同的错误”,但我们需要看看实际的错误是什么。
【解决方案2】:

没有 logcat 很难调查问题

但是,您可以通过在清单中添加配置更改来跳过轮换重启

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

【讨论】:

  • 这会影响布局设计
  • Ramadan.java 中的第 125 行是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
相关资源
最近更新 更多