【问题标题】:Android sharedPreferences is not saving or getting strings properlyAndroid sharedPreferences 未正确保存或获取字符串
【发布时间】:2014-03-03 03:51:03
【问题描述】:

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

问题是我从未保存过它。我有保存功能,但从未将其保存到偏好设置中。

从我保存的首选项中获取字符串时,我得到了空返回。我不确定 savedpreferences 是如何工作的,但我的理解是,当调用 sharedpreferences 时,它会在手机上创建密钥对文件,以便您稍后再使用它。

我的程序本质上是一个字符串创建应用程序。当您按下按钮时,它会创建一个字符串以作为短信发送。我的设置活动页面有四个编辑文本,可以通过单击按钮保存其中的任何内容并返回到主活动。最后一个按钮通过从键值对中获取值来创建一个字符串并构造消息。但是,对于每个值,我总是得到 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(); 
  }
}

【问题讨论】:

  • 代替Editor,试试SharedPreferences.Editor
  • 现在改变这个......让我们看看。不。改变它没有任何作用。
  • 你在哪里调用了这个save(View view) 方法?或者把你所有的代码?
  • 我编辑了帖子,我放了一个链接并发布了保存方法。第一个代码块中的 save 方法是一个 save(view view) 方法,它调用那里第二个代码块中的最后一个代码块。

标签: android sharedpreferences


【解决方案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】:

    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);
    

    【讨论】:

      【解决方案3】:

      改变这个

      sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      

      sp = getPreferences(0);
      

      【讨论】:

      • 让我试试这个,我在两个活动中都更改了 onCreate 方法中的两行,遗憾的是,它没有改变任何东西。
      • 发送了一个包含项目文件链接的 pastebin 以及发布在我的 Dropbox 上的整个项目本身:pastebin.com/KMfZKSqf
      猜你喜欢
      • 2012-05-30
      • 2014-03-01
      • 2014-12-25
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多