【问题标题】:I am trying to pass the values in shared preference from one Activity to another我正在尝试将共享偏好中的值从一个活动传递到另一个活动
【发布时间】:2017-09-02 06:13:50
【问题描述】:

我正在尝试使用共享首选项将值从一个活动传递到另一个活动,但得到的是空值。在第一个活动中,我在控制台中打印了值并打印了它,但我无法检索这些值。请帮助我要摆脱这个错误

第一个活动:传递值

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



}

【问题讨论】:

  • logcat也有错误吗?
  • 我想把这个sharedpreferences.edit().putString("CHECKPASS","changepass").commit();改成简单的editor.putString("CHECKPASS","changepass");
  • @Mandy8055 logcat 中没有错误。但是控件在第二个活动中没有转到 (succ", "reached)。我该怎么办。
  • 好的!!你试过@ModularSynth提到的解决方案吗?
  • 请提供minimal reproducible example这并不意味着发布您的整个应用程序。相反,您需要确保包含所有相关的类、方法以及变量声明和初始化。我应该能够复制/粘贴您的代码,然后编译并运行它而无需任何更改,并获得您所询问的完全相同的行为。

标签: android sharedpreferences


【解决方案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(),因为它在后台处理数据。

【讨论】:

    【解决方案2】:

    更新

    在你的第一个活动中

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

    在你的第二个活动中

     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
    

    【讨论】:

    • 我应该在哪里使用这个..在这两个活动中?
    • 在您的第二个活动中并使用 getstring 的设置\
    • 并且没有“NULL”它唯一的null
    • 不使用设置它只是名称你使用你想要使用的共享首选项
    猜你喜欢
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2017-12-10
    • 2012-07-05
    相关资源
    最近更新 更多