2 clcurie 11 clcurie_11 于 2013.04.26 17:07 提问

不同 activity 中的 SharedPreferences

我用下面的代码从 activity A 中加载 SharedPreferences:

private void SavePreferences(String key, String value){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }

在 activity B 中我想加载 SharedPreferences。下面是一个异常 NullPointerException:

private void LoadPreferences(){   
       SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
       data = sharedPreferences.getString("name", "08:00") ;
   }

我用下面的代码,得到这个编译错误:"No enclosing instance of the type A is accessible in scope"

 private void LoadPreferences(){   
       SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(A.this);
       data = sharedPreferences.getString("name", "08:00") ;
   }

如何访问数据?谢谢!

2个回答

zxd_72
zxd_72   2013.04.28 10:46
已采纳

在两个 Activities 中把 this 替换为 getApplicationContext()
在Activity A 的 SharedPreferences 是下面的方式:

private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
        Intent sd=new Intent(this,Secongtess.class);
        startActivity(sd);
       }

Activity B 中按下面的方式获取值:

 private void LoadPreferences(){   
       SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
     String  data = sharedPreferences.getString("name", "08:00") ;
     Toast.makeText(this,data, Toast.LENGTH_LONG).show();
   }
CSDN_Ask
CSDN_Ask   2013.04.28 10:50

在 shared preferences 中存储值

SharedPreferences preferences==PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor=preferences.edit();
  editor.putString("Name","Harneet");
  editor.commit();

从 shared preferences 中检索值:

SharedPreferences preferences==PreferenceManager.getDefaultSharedPreferences(this);
  String name=preferences.getString("Name","");
  if(!name.equalsIgnoreCase(""))
  {
    name=name+"  Sethi";  /* 编辑值 */
  }

从 sharedpreference 中编辑数据

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.commit();

从 shared preference 中检索数据

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) 
{
  //mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
  int selectionStart = prefs.getInt("selection-start", -1);
  int selectionEnd = prefs.getInt("selection-end", -1);
  /*if (selectionStart != -1 && selectionEnd != -1)
  {
     mSaved.setSelection(selectionStart, selectionEnd);
  }*/
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!