2

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
dhun
  • 643
  • 7
  • 13
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – 323go Mar 27 '15 at 18:13
  • But why does it go and reach that point? I have already locked the app to landscape. – dhun Mar 27 '15 at 18:15
  • Debug/trace your code and find out why it recreates the activity. Make sure you handle it correctly. – 323go Mar 27 '15 at 18:16
  • I tried a lot! Is it device's lock screen? Possible? – dhun Mar 27 '15 at 18:19
  • And I am not concerned about Null pointer exception, I know how to fix it, please don't -1 my question at least. It is just I want to know why such behavior and fix it. – dhun Mar 27 '15 at 18:21
  • So the real question is, why is your app going to portrait mode when you've locked it into landscape? – Eric S. Mar 27 '15 at 18:22
  • Where did you put android:screenOrientation="landscape"? – Eric S. Mar 27 '15 at 18:22
  • In my AndroidManifest.xml, inside each activity tags. And it works fine if I open and run one activity and change the orientation. The problem happens only when I lock the screen of the device with lock keys and unlock it. No matter whichever activity is opened, the application crashes. – dhun Mar 27 '15 at 18:25
  • wait, do you want to prevent orientation change or not? – Eric S. Mar 27 '15 at 18:34
  • Yes I do, I don't want to rotate my application to portrait mode at all. But device can rotate to portrait mode. The whole device is not locked down to any specific orientation. – dhun Mar 27 '15 at 18:37

1 Answers1

5

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" >
dhun
  • 643
  • 7
  • 13
  • Thanks for sharing your solution, solved the same problem with a project using pgs4a; only had to change the target API to 13 - the orientation and screenSize were introduced then. – Ireneusz Sep 17 '17 at 12:35