Android - Saving an object in onSaveInstanceState?
Posted
by Donal Rafferty
on Stack Overflow
See other posts from Stack Overflow
or by Donal Rafferty
Published on 2010-06-17T16:31:43Z
Indexed on
2010/06/17
16:33 UTC
Read the original article
Hit count: 648
I have created a small XML parsing application for Android that displays information in a listview and then allows a user to click on the list view and a dialog with further info will pop up.
The problem is that when the screen orientation is changed when a dialog screen is open I get a null pointer error.
The null pointer occurs on the following line:
if(setting.getAddForPublicUserNames() == 1){
This line is part of my dialogPrepare method:
@Override
public void onPrepareDialog(int id, Dialog dialog) {
switch(id) {
case (SETTINGS_DIALOG) :
afpunText = "";
if(setting.getAddForPublicUserNames() == 1){
afpunText = "Yes";
}
else{
afpunText = "No";
}
String Text = "Login Settings: " + "\n"
+ "Password: " + setting.getPassword() + "\n"
+ "Server: " + setting.getServerAddress() + "\n"
+ "Register: " + setting.getRegistrarAddress() + "\n"
+ "Realm: " + setting.getRealm() + "\n"
+ "Public UserNames: " + afpunText + "\n"
+ "Preference Settings: " + "\n"
+ "Request VDN: " + setting.getRequestVDN() + "\n"
+ "Handover Settings: " + "\n"
+ "Enable Handover: " + setting.getEnableHandover() + "\n"
+ "Hand Over Number: " + setting.getHandoverNum() + "\n";
AlertDialog settingsDialog = (AlertDialog)dialog;
settingsDialog.setTitle("Auth ID: " + setting.getUserName());
tv = (TextView)settingsDialog.findViewById(R.id.detailsTextView);
if (tv != null)
tv.setText(Text);
break;
}
}
So the error is that my variable setting is null after the screen orientation changes.
I have tried to use the onSaveInstance state methods to fix that as follows:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
for(int i = 0; i < settings.size(); i++){
savedInstanceState.putString("Username"+i, settings.get(i).getUserName());
savedInstanceState.putString("Password"+i, settings.get(i).getPassword());
savedInstanceState.putString("Server"+i, settings.get(i).getServerAddress());
savedInstanceState.putString("Registrar"+i, settings.get(i).getRegistrarAddress());
savedInstanceState.putString("Realm"+i, settings.get(i).getRealm());
savedInstanceState.putInt("PUserNames"+i, settings.get(i).getAddForPublicUserNames());
savedInstanceState.putString("RequestVDN"+i, settings.get(i).getRequestVDN());
savedInstanceState.putString("EnableHandOver"+i, settings.get(i).getEnableHandover());
savedInstanceState.putString("HandOverNum"+i, settings.get(i).getHandoverNum());
}
super.onSaveInstanceState(savedInstanceState);
}
and
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//Check to see if this is required
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
for(int i = 0; i<settings.size(); i++){
settings.get(i).setUserName(savedInstanceState.getString("Username"+i));
settings.get(i).setPassword(savedInstanceState.getString("Password"+i)) ;
settings.get(i).setServerAddress(savedInstanceState.getString("Server"+i));
settings.get(i).setRegistrarAddress(savedInstanceState.getString("Registrar"+i));
settings.get(i).setRealm(savedInstanceState.getString("Realm"+i));
settings.get(i).setAddForPublicUserNames(savedInstanceState.getInt("PUserNames"+i));
settings.get(i).setRequestVDN(savedInstanceState.getString("RequestVDN"+i));
settings.get(i).setEnableHandover(savedInstanceState.getString("EnableHandOver"+i));
settings.get(i).setHandoverNum(savedInstanceState.getString("HandOverNum"+i));
}
}
However the error still remains, I think I have to save the selected setting from what was selected from the ListView? But how do I save a setting object in onSavedInstance?
© Stack Overflow or respective owner