2017-09-02 22 views
1

我试图使用共享偏好从一个活动的值传递到另一个,但我得到空value.In第一活动i的控制台和其打印的值越来越打印,但在这我无法检索values.please帮我走出这个错误我试图从一个活动传递共享的偏好的值到另一个

第一项活动组成:值传递

sharedpreferences.edit().putString("CHECKPASS","changepass").commit(); 
editor.putString("FOOD",food1); 
editor.putString("PLACE",place1); 
editor.putString("COLOR",colour1); 
editor.putString("BUY",buy1); 
editor.commit(); 

次活动:我在哪里检索

 Log.d("succ", "reached"); 
     String yourpass = sharedpreferences.getString("CHECKPASS","changepass"); 
     Log.d("succ", "yournext" + yourpass); 

     if (yourpass.equals("changepass")) { 
      { 
       final String foodshared = sharedpreferences.getString("FOOD","NULL"); 
       Log.d("succ", "foodshared" + foodshared); 

       final String colorshared = sharedpreferences.getString("COLOR", "NULL"); 
       Log.d("succ", "colorshared" + colorshared); 

       final String buyshared = sharedpreferences.getString("BUY", "NULL"); 
       Log.d("succ", "buyshared" + buyshared); 

       final String placeshared = sharedpreferences.getString("PLACE", "NULL"); 
       Log.d("succ", "placeshared" + placeshared); 



} 
+0

有没有在'logcat'过任何错误? – Mandy8055

+0

我会尝试改变这种'sharedpreferences.edit()putString( “CHECKPASS”, “changepass”)提交();'简单'editor.putString( “CHECKPASS”, “changepass”);' –

+0

@ Mandy8055在logcat中没有错误。但是在第二项活动中,控制权并未达到(succ“,”达到),我该怎么做。 – shwettha

回答

0

UPDATE

在你的第一个活动

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);//or MODE_PRIVATE 
Editor editor = pref.edit(); 
editor.putString("CHECKPASS","changepass") 
editor.putString("FOOD",food1); 
editor.putString("PLACE",place1); 
editor.putString("COLOR",colour1); 
editor.putString("BUY",buy1); 
editor.commit(); 

在你secondActivity

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 

final String foodshared = pref.getString("FOOD",null); 
      Log.d("succ", "foodshared" + foodshared); 

      final String colorshared = pref.getString("COLOR",null); 
      Log.d("succ", "colorshared" + colorshared); 

      final String buyshared = pref.getString("BUY",null); 
      Log.d("succ", "buyshared" + buyshared); 

      final String placeshared = pref.getString("PLACE",null); 
      Log.d("succ", "placeshared" + placeshared); 

编辑

初始化

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
Editor editor = pref.edit(); 

存储数据

editor.putBoolean("key_name", true); // Storing boolean - true/false 
editor.putString("key_name", "string value"); // Storing string 


editor.commit(); // commit changes 

检索数据

pref.getString("key_name", null); // getting String 
pref.getBoolean("key_name", null); // getting boolean 
+0

我应该在哪两个活动中使用这个.. – shwettha

+0

在您的第二个活动和使用设置为getstring \ –

+0

,并没有“NULL”它的唯一空 –

1

使用此代码

//将数据写入

SharedPreferences.Editor 
editor=getSharedPreferences("nameOFSharedPref",MODE_PRIVATE).edit(); 
editor.putString("CHECKPASS","changepass"); 
editor.putString("FOOD",food1); 
editor.putString("PLACE",place1); 
editor.putString("COLOR",colour1); 
editor.putString("BUY",buy1); 
editor.apply(); 

//读取数据

SharedPreferences sharedPreferences=getSharedPreferences("nameOFSharedPref",MODE_PRIVATE); 
String foodshared = sharedPreferences.getString("FOOD","NULL"); 
String colorshared = sharedPreferences.getString("COLOR", "NULL"); 
final String buyshared = sharedPreferences.getString("BUY", "NULL"); 
final String placeshared = sharedPreferences.getString("PLACE", "NULL"); 

而且写入数据时不使用editor.commit(),而不是使用editor.apply(),因为它在后台处理数据。

相关问题