Reputation: 547
I've got an activity which should have two rather different layouts depending on whether the user holds it in landscape or portrait mode.
In onRetainNonConfigurationInstance(), I store a lot of information and extract this in onCreate() if it exists, to restore the user data.
I've got a method to determine if the phone is currently in portrait or landscape mode - if getWindowManager().getDefaultDisplay().getWidth() is larger than getWindowManager().getDefaultDisplay().getHeight(), then it's in landscape.
In my portrait layout xml file, I have a few image views which are not present in the landscape layout xml file. In onCreate(), I check if we're in portrait, then initialise and modify these if so. If we're in landscape mode, I don't.
This all works fine for switching from portrait to landscape, but then when switching back into portrait, my code that is executed only for portrait mode gives a null pointer exception - it can't find the view elements.
Stack trace:
E/AndroidRuntime( 4800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.hyperview/com.apps.hyperview.PopUp}: java.lang.NullPointerException
E/AndroidRuntime( 4800): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
E/AndroidRuntime( 4800): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
E/AndroidRuntime( 4800): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3278)
E/AndroidRuntime( 4800): at android.app.ActivityThread.access$1900(ActivityThread.java:112)
E/AndroidRuntime( 4800): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
E/AndroidRuntime( 4800): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4800): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 4800): at android.app.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime( 4800): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4800): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 4800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime( 4800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
E/AndroidRuntime( 4800): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 4800): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 4800): at com.apps.hyperview.PopUp.onCreate(PopUp.java:147)
E/AndroidRuntime( 4800): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
E/AndroidRuntime( 4800): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
Does anyone have any ideas what I'm doing wrong?
Thanks!! r3mo
EDIT:
Popup.java section causing problems:
// Check orientation. If we're in portrait...
if( isInPortrait() )
{
spacer_above = (ImageView) findViewById(R.id.image_display_popup_spacer_above);
spacer_above.setVisibility(View.INVISIBLE); // THIS IS CAUSING THE NULLPOINTER
spacer_below = (ImageView) findViewById(R.id.image_display_popup_spacer_below);
spacer_below.setVisibility(View.INVISIBLE);
}
else;
The portrait version of the layout file contains these 'spacers', but the landscape version does not. Is this how orientation changes and different layouts should be handled when using onRetainNonConfigurationInstance()? The code all works fine going from portrait to landscape, but when going back to portrait, I get the NullPointerException.
EDIT 2:
isInPortrait() seems to be working fine - I will change this however. The reason I'm not just making the views invisible is that the structure of the layout is significantly different in landscape. Obviously, this landscape layout is working fine - the error occurs when going from portrait to landscape and then back to portrait.
All help is appreciated!
EDIT 3:
I've now changed how I'm doing this to have the same layout file in layout-port/ and layout-land/, and making the view elements that I don't want in landscape View.GONE. It would certainly still be good to get input on this problem - it seems like a very powerful feature of android to be able to have completely different layouts for landscape and portrait.
Thanks!
Upvotes: 0
Views: 2195
Reputation: 234847
You can put the portrait layout in res/layout-port, the landscape version of the layout in res/layout-land, and a default (for square screens, I suppose) in res/layout. The system automatically (and reliably) determines which version of the layout to use. Is there a reason that you aren't doing that?
Upvotes: 0