Search code examples
androidrotationscreen-orientation

Android App orientation locked to landscape, locking the device, rotate it to portrait and unlock it, Application crash


My application is related to Music and I am using Android 4.2.2. I have locked my application to landscape mode by specifying below line in each of my activity:

android:screenOrientation="landscape"

Now, when I keep my Music application open in landscape mode, lock the screen of the device with the keys, rotate the device in portrait mode and then unlock the screen of the device will cause my Music application throw an Exception.

I tried multiple solution like setting the configChanges in my manifest

android:configChanges="keyboardHidden|orientation|screenSize" 

But nothing seem to work. I am posting the stack trace of the exception here. Please help me with this issue.

Note : Keeping the music application in landscape mode, locking the device and unlocking it will do nothing to the application. App works like before and also displays the activity which was opened before locking the device.

What I want to do: I want to lock my application (just my app and not the whole device) to landscape mode and at the same time don't listen to any orientation change of the device.

Thanks!

Stacktrace:

E/AndroidRuntime(17468): FATAL EXCEPTION: main
E/AndroidRuntime(17468): java.lang.RuntimeException: Unable to start activity ComponentInfo com.android.music/com.android.music.MediaPlaybackActivity}: java.lang.NullPointerException
E/AndroidRuntime(17468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(17468):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(17468):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
E/AndroidRuntime(17468):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
E/AndroidRuntime(17468):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
E/AndroidRuntime(17468):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(17468):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(17468):    at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(17468):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(17468):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(17468):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(17468):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(17468):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(17468): Caused by: java.lang.NullPointerException
E/AndroidRuntime(17468):    at com.android.music.MediaPlaybackActivity.onCreate(MediaPlaybackActivity.java:232)
E/AndroidRuntime(17468):    at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(17468):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(17468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
E/AndroidRuntime(17468):    ... 12 more

Solution

  • Might be helpful for someone else having the same problem..

    So, Below line works (Though it is not a very good solution to handle this type of changes)

     android:configCganges="orientation/screenSize"
    

    It did not work for me before because I did not put it right after providing the activity's name. So it will not work if it is not in a proper order. (At least for Android 4.2.2)

    For example, Below code works.

         <activity 
                android:name="com.android.music.MediaPlaybackActivity"
                android:configChanges="orientation|screenSize"
                android:screenOrientation="landscape"
                android:exported="true" >