Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:33:37+00:00 2026-05-28T05:33:37+00:00

I’m having some trouble restored SharedPreferences into an activity separate from the original. I

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:33:38+00:00Added an answer on May 28, 2026 at 5:33 am

    It looks like you’re trying to get the preferences in oldCustomerActivity using 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.