egydeveloper
egydeveloper

Reputation: 585

the application stopped when stop playing the sound

I created my Android app to play sound when user select option value on i did my code well. All thing worked well but when i added my code to make user mute or stop the sound when click on lock button at his mobile it worked well but when i rotate the mobile horizontal the error " the application stopped unexpectedly " occurred. I checked my code and i reached that the error at onPause method .

@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

Upvotes: 0

Views: 116

Answers (2)

Peter Lo
Peter Lo

Reputation: 110

Hard to investigate the problem without logcat

However, you can skip the restart on rotate by adding config change in Manifest

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

Upvotes: 0

devunwired
devunwired

Reputation: 63303

You will get lots more information if you look at the logcat output from your crash, as it will give you the reason for the exception. However, just looking at the above code, you cannot call registerReceiver() and unregisterReceiver() with different instances, they must be the same object. The call to unregisterReceiver() is likely filing with an exception saying that receiver instance was never registered before. Your code needs to look more like this:

//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!
}

Upvotes: 3

Related Questions