Search code examples
androidemulationlogcatscreen-orientationmode

My android app crashes on screen rotation


I am fairly new to android development and am developing my first android app. I am almost finished with my coding but when I run the app on landspace mode it force closes or even if I run on portrait mode and rotate the emulator the app still crashes. It works fine on portrait mode. I am not really sure what is causing this to happen but in my logcat, I am getting the following.

12-12 18:33:00.074: D/AndroidRuntime(1014): Shutting down VM
12-12 18:33:00.074: W/dalvikvm(1014): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-12 18:33:00.104: E/AndroidRuntime(1014): FATAL EXCEPTION: main
12-12 18:33:00.104: E/AndroidRuntime(1014): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplecounter/com.main.simplecounter.MainActivity}: java.lang.NullPointerException
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.os.Looper.loop(Looper.java:123)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at java.lang.reflect.Method.invoke(Method.java:521)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at dalvik.system.NativeStart.main(Native Method)
12-12 18:33:00.104: E/AndroidRuntime(1014): Caused by: java.lang.NullPointerException
12-12 18:33:00.104: E/AndroidRuntime(1014):     at com.main.simplecounter.MainActivity.appBackground(MainActivity.java:252)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at com.main.simplecounter.MainActivity.setUp(MainActivity.java:78)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at com.main.simplecounter.MainActivity.onCreate(MainActivity.java:48)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-12 18:33:00.104: E/AndroidRuntime(1014):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-12 18:33:00.104: E/AndroidRuntime(1014):     ... 11 more
12-12 18:38:00.158: I/Process(1014): Sending signal. PID: 1014 SIG: 9

I have searched online and tried out few solutions but none have worked. For example I added this to my manifest but it still crashes.

 <activity
    android:configChanges="orientation|keyboardHidden|screenSize"
</activity>

If someone can help me out, it would be great.

Thanks for your time and I hope I have explained my problem clearly.


Solution

  • Yes, I have a different layout for my landspace mode

    Probably you don´t have specified into your res/layout-land layout the element with id: layout, so you are getting a java.lang.NullPointerException