0

我创建了我的Android应用程序以在用户选择选项值时播放声音,我的代码做得很好。一切运行良好,但是当我添加我的代码以使用户在点击他的手机上的锁定按钮时静音或停止声音时,它运行良好,但是当我将手机水平旋转时,出现“应用程序意外停止”错误。我检查了我的代码,并在方法中发现了错误。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
4

2 回答 2

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!
}
于 2013-06-24T23:00:45.310 回答
0

没有 logcat 很难调查问题

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

    <activity
        android:name="com.youclassname.xxx"
        android:configChanges="orientation|keyboardHidden|screenSize"/>
于 2013-06-24T23:03:35.057 回答