I’m having some trouble restored SharedPreferences into an activity separate from the original.
I have two classes that are utilizing this, “NewCustomerActivity” and “OldCustomerActivity”. Both should have read/write access to the file – currently I’m just testing by writing from NewCustomerActivity, which will appropriately restore form data upon killing the activity; however I’m receiving a FC upon opening OldCustomerActivity which is attempting to restore the data in the same sense as NewCustomerActivity with a NullPointerException.
NewCustomerActivity:
public class NewCustomerActivity extends Activity {
public static final String USER_INFO = "UserInfoFile"; // This file will the store the user's information for later use.
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newcustomer);
SharedPreferences userInfo = getSharedPreferences(USER_INFO, 0); // Restore our earlier saved info.
EditText et;
...... (Other code is irrelevant)
}
OldActivityNew is the same:
public class OldCustomerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oldcustomer);
SharedPreferences userInfo = getSharedPreferences(NewCustomerActivity.USER_INFO, 0); // Restore our earlier saved info.
EditText et;
et = (EditText) findViewById(R.id.EditTextName);
et.setText(userInfo.getString("name",""));
..... (Continue to fill forms in this context)
}
Both of the activities fill the form with previous information, and update the file upon submission if anything has changed; however only NewCustomerActivity seems to be populating the file (Or not force closing to be specific)
I’ve tried setting up the SharedPreference in MODE_WORLD_READABLE (second parameter = 1) as well to no avail; even though I believe I should be able to run it in PRIVATE as it is. I’ve also tried referencing USER_INFO as NewCustomerActivity.USER_INFO
I must be missing something obvious – but any help would be appreciated as this is my first attempt, thanks!
Edit:
For those asking how I am writing to the file:
SharedPreferences userInfo = getSharedPreferences(USER_INFO, 0); // Save info for later
SharedPreferences.Editor userInfoEditor = userInfo.edit();
EditText et;
et = (EditText) findViewById(R.id.EditTextName);
String nameValue = et.getText().toString();
userInfoEditor.putString("name", nameValue);
It looks like you’re trying to get the preferences in
oldCustomerActivityusing a different mode than that which you wrote them.Change this line in that class:
getSharedPreferences(NewCustomerActivity.USER_INFO, 1)to use a 0 for the mode parameter:
getSharedPreferences(NewCustomerActivity.USER_INFO, 0)The int you pass to the function doesn’t define if you are reading or writing, but defines how you want the preferences to behave after you first write them.
To edit the preferences after you have loaded them you need to call:
SharedPreferences.Editor editor = userInfo .edit();You can then set the variables like this:
editor.putString("username", "Ash");More information can be found here.