简体   繁体   中英

Android Java - Shared Preferences Editor Issue

I'am trying to permentantly store values from 3 differenct strings into Shared Preferences. The strings definately contain the values as i have tested them by setting the strings in a textView.

public class verified extends Activity{

    SharedPreferences sharedpreferences;
    TextView textView100;
     String MyPREFERENCES = "MyPreferences" ;

        String NString;
        String MString;
        String EAString;

           public static final String MVerified = "";
           public static final String NVerified = "";
           public static final String EAVerified = "";



    public void onCreate(Bundle savedInstanceState)

     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verified);

  sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

         Bundle bundle = getIntent().getExtras();
         NString = bundle.getString("NString");
     MString = bundle.getString("MString");
     EAString = bundle.getString("EAString");

      textView100 = (TextView) findViewById(R.id.textView100);

    textView100.setText(MString + " , " + NString + " , " + EAString);


        Button VCompleteButton = (Button) findViewById(R.id.VCompleteButton);

        VCompleteButton.setOnClickListener(new View.OnClickListener()

        {
           public void onClick(View view) 
           {    



             Editor editor = sharedpreferences.edit();
             editor.putString(MVerified, MString);
             editor.putString(NVerified, NString);
             editor.putString(EAVerified, EAString);
             editor.commit(); 


           }



           });


 }



}

However the issue now is that the EAString is storing its value in all three of the. sharedPreferences. When I execute this on the main activity. It shows me the EAStrin 3 times.

textView2.setText((sharedpreferences.getString(MobileVerified, "")) + " , "+ (sharedpreferences.getString(NameVerified, "")) +  " , "+ (sharedpreferences.getString(EmailAddressVerified, "")));

you never initiate sharedpreferences :

SharedPreferences sharedpreferences;

thats the reason why you get a NullPointerException here:

Editor editor = sharedpreferences.edit();

put something like this in your onCreate method before using SharedPreferences:

sharedpreferences = this.getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);

that are your keys:

public static final String MVerified = "";
           public static final String NVerified = "";
           public static final String EAVerified = "";

each key is an empty String. You have to define a key for each entry in your SharedPreferences:

public static final String MVerified = "key1";
           public static final String NVerified = "key2";
           public static final String EAVerified = "key3";

otherwise you overwrite each entry with the next one...

You have declared but not initiated SharedPreferences. Do this:

SharedReferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); // common instance that will be shared for this context

It looks to me as if your not including the @Override annotation for your onCreate(Bundle savedInstanceState) Method. Nor do you include this same annotation when overriding the View s onClick(View v) method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM