Android sharedPreferences未正确保存或获取字符串

时间:2014-03-03 03:51:04

标签: android sharedpreferences

编辑:用户Guardanis在其他地方的评论解决了这个问题。 Android SharedPreferences not loading and saving properly

问题在于我从来没有说过它。我有一个保存功能,但从未将其保存到偏好设置中。

我从保存的偏好设置中获取字符串时返回null。我不确定保存的偏好是如何工作的,但我的理解是,当调用共享偏好时,它会在手机上创建密钥对文件,以便您稍后再回来。

我的程序本质上是一个字符串创建应用程序。当您按下按钮时,它会创建一个要作为短信发送的字符串。我的设置活动页面有四个edittexts,通过buttonclick保存其中的任何内容并返回主活动。最后一个按钮通过从keyvalue对获取值并构造消息来创建String。但是,我总是为每个值得到null。

继承设置页面的代码,然后是主页面。请问我是否可以添加更多,我没有添加所有代码,只是共享偏好部分。

public SharedPreferences sp;
public Editor e;

public void savethethings(){ //run this when enter is pressed/savew
    EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
    EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
    EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
    EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);


    String introstring = smsintro_hint.getText().toString();
    String bodystring = smsbody_hint.getText().toString();
  String checkboxbodystring = checkboxbody_hint.getText().toString();
  String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();


      e.putString("intro", introstring);
      e.commit(); // you forgot to commit


  if(!bodystring.isEmpty())
  {
      e.putString("body", bodystring);
      e.commit(); 
  }

  if(!checkboxbodystring.isEmpty())
  {
      e.putString("checkbody", checkboxbodystring);
      e.commit(); 
  }

  if(!checkboxdescriptionstring.isEmpty())
  {
      e.putString("checkboxdescr", checkboxdescriptionstring);
      e.commit(); 
  }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.settingmenu);

    //SP
    sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // forget about
    // named preferences - get the default ones and finish with it
    e = sp.edit();

    Button tt = (Button)findViewById(R.id.savebutton);
    tt.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {
                finish();

        }
    });


public void save(View view)
{
    //THINGS HAPPEN HERE WITH SHARED PREFERENCES :(
    savethethings();

    this.finish();
    return;
}

public String finishedtext(String userstring)
{

    smsintroduction = (sp.getString("intro", ""));
    smsbody = (sp.getString("body", ""));
    checkboxtext = (sp.getString("checkbody", ""));
    checkboxmessage = (sp.getString("checkboxdescr", ""));

    if(smsintroduction.isEmpty()) 
    {
        if(smsbody.isEmpty())
        {
            if(checkboxtext.isEmpty())

            {
                if(checkboxmessage.isEmpty()) //topkek for most AND statements Ive ever put in in if/then form
                {
                    //Essentially the DEFAULT if they're ALL null
                    smsbody = "Hi "+ userstring +"! This is coming from jake's phone and it wants to send a text so we can talk or whatever. ";
                }
            }
        }
    }




    Toast.makeText( this, "Creating text, then press send!", Toast.LENGTH_LONG).show();

    String thetext = "";

    thetext = smsintroduction + " " + smsbody + " " + checkboxtext;

    return thetext;
}

public void savethethings(){ //run this when enter is pressed/savew
    EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
    EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
    EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
    EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);


    String introstring = smsintro_hint.getText().toString();
    String bodystring = smsbody_hint.getText().toString();
  String checkboxbodystring = checkboxbody_hint.getText().toString();
  String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();

  //      if(!introstring.isEmpty()) //if the fields are NOT empty, they should get saved.
  //      {
      e.putString("intro", introstring);
      e.commit(); // you forgot to commit


  if(!bodystring.isEmpty())
  {
      e.putString("body", bodystring);
      e.commit(); 
  }

  if(!checkboxbodystring.isEmpty())
  {
      e.putString("checkbody", checkboxbodystring);
      e.commit(); 
  }

  if(!checkboxdescriptionstring.isEmpty())
  {
      e.putString("checkboxdescr", checkboxdescriptionstring);
      e.commit(); 
  }
}

3 个答案:

答案 0 :(得分:1)

更改此

sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

sp = getPreferences(0);

答案 1 :(得分:1)

试试这个。

//declareation
    private SharedPreferences _sPrefs=null;
    //do this in oncreate
    _sPrefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
    //this is for your editor 
    SharedPreferences.Editor prefEditor = _sPrefs.edit();

答案 2 :(得分:1)

SharedPreferences类提供了一个通用框架,允许您保存和检索原始数据类型的持久键值对。您可以使用SharedPreferences保存任何原始数据:布尔值,浮点数,整数,长整数和字符串。

您可以保存以下四个文本:

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
shared.edit().putString("text_name1", "value1").commit();

然后在活动中(你需要让他们回来)

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
String text_name1=shared.getString("text_name1", null);