2012-07-03 12 views
18

me carga de la actividad A los SharedPreferences en siguiente manera:SharedPreferences de diferente actividad

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

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

En actividad B I desea cargar el SharedPreferences. A continuación fue una NullPointerException:

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

si intento siguiente, me sale este error de compilación: "Ningún caso encerrando del tipo A es accesible en su alcance"

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

¿Cómo puedo acceder a los datos?

Respuesta

32

uso getApplicationContext() en lugar de this tanto en las actividades como:

En actividad A los SharedPreferences en siguiente manera:

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

y en la actividad B obtener el valor como:

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

porque como dice el documento:

getDefaultSharedPreferences(Context context):

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.

+1

me cambió "esto" a getApplicationContext(), y ahora funciona. ¡Muchas gracias! – user1390816

+1

no funciona ........... –

+0

^En algún momento es posible que deba pasar el objeto SharedPreference cuando llame a una función de la actividad A a B. –

2

para almacenar valores en las preferencias compartidas:

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

Para recuperar los valores de las preferencias compartidas:

SharedPreferences preferences==PreferenceManager.getDefaultSharedPreferences(this); 
    String name=preferences.getString("Name",""); 
    if(!name.equalsIgnoreCase("")) 
    { 
    name=name+" Sethi"; /* Edit the value here*/ 
    } 

Para editar los datos de 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(); 

Para recuperar datos de preferencia compartida

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); 
    }*/ 
} 
Cuestiones relacionadas