tags:

views:

8224

answers:

2

Hi

I am using Intents to switch between activities in my Android app. I am putting data in the Intent for use in the next activity. When I switch the phone between landscape and portrait modes, the values passed from the intent are lost and I get a NullPointerException.

Can someone please tell me what could be wrong.

There's a lot of code to post it entirely. But if someone needs to look at specific parts of code, I can post it here.

Edit
I solved the issue of state not being saved. But another problem I faced is that none of the buttons on the screen work after the orientation has been changed. On button press, I get this warning in LogCat

02-25 23:07:49.190: WARN/WindowManager(58): No window to dispatch pointer action 0

Please help.

+7  A: 

When you switch orientation the activity is recreated and onCreate is recalled so you have to use the bundle to save your current state and restore after an orientation change. You can see this in action if you have just an app with a TextView and you enter text and change orientation. If you bundle your state for onCreate you can curb this. This is probably also why you have a NullPointer after the orientation changes. It is annoying as all hell but something we have to live with.

This link on a series of orientation tutorials and this first one in particular should help you understand exactly what is going on and how to successfully maintain your current state.

Update: There is also a post on SO Activity restart on rotation Android that deals with almost the same thing.

Edit for follow up question:

Did you re-attach your click handlers after the orientation change?

Quintin Robinson
A: 

@Override
public void onCreate(Bundle savedInstanceState) {
... somevalue = savedInstanceState.getString(SOME_KEY);
...
}
...
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(SOME_KEY, "blah blah blah");
}

try this