简体   繁体   English

Android Java-共享的首选项编辑器问题

[英]Android Java - Shared Preferences Editor Issue

I'am trying to permentantly store values from 3 differenct strings into Shared Preferences. 我试图将3个不同字符串的值存储到共享首选项中。 The strings definately contain the values as i have tested them by setting the strings in a textView. 字符串绝对包含值,因为我已经通过在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. 但是,现在的问题是EAString将其值存储在所有三个中。 sharedPreferences. sharedPreferences。 When I execute this on the main activity. 当我在主要活动上执行此操作时。 It shows me the EAStrin 3 times. 它向我显示了EAStrin 3次。

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

you never initiate sharedpreferences : 您永远不会启动sharedpreferences

SharedPreferences sharedpreferences;

thats the reason why you get a NullPointerException here: 多数民众赞成在这里得到NullPointerException的原因:

Editor editor = sharedpreferences.edit();

put something like this in your onCreate method before using SharedPreferences: 在使用SharedPreferences之前,在您的onCreate方法中放入以下内容:

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: 您必须为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. 您已经声明但尚未启动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. 在我看来,好像您的onCreate(Bundle savedInstanceState)方法不包含@Override注释。 Nor do you include this same annotation when overriding the View s onClick(View v) method. 重写ViewonClick(View v)方法时,也不会包含相同的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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