Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SharedReferences在重新打开应用程序后未保存_Android - Fatal编程技术网

Android SharedReferences在重新打开应用程序后未保存

Android SharedReferences在重新打开应用程序后未保存,android,Android,我试图添加一个名为removeCard的函数,该函数接收一个字符串数组。此数组长度为48,数组中的字符串包含卡的名称,即王子或骑士。如果此数组中的字符串为null,则表示没有更多的卡可删除(游戏包含48张卡,但可能我只想删除其中的2张) 我的所有卡都用key1键保存在SharedReferences中。此函数用于从密钥中移除阵列中的所有卡(如果有人意外添加了王子,但他尚未解锁) 但是,此函数工作正常,当我返回到main3.class意图时,它显示阵列中的卡已从SharedReferences键中

我试图添加一个名为
removeCard
的函数,该函数接收一个字符串数组。此数组长度为48,数组中的字符串包含卡的名称,即王子或骑士。如果此数组中的
字符串
null
,则表示没有更多的卡可删除(游戏包含48张卡,但可能我只想删除其中的2张)

我的所有卡都用
key1
键保存在
SharedReferences
中。此函数用于从密钥中移除阵列中的所有卡(如果有人意外添加了王子,但他尚未解锁)

但是,此函数工作正常,当我返回到
main3.class
意图时,它显示阵列中的卡已从
SharedReferences
键中移除,但当我关闭应用程序并重新打开它时,它们仍然像从未移除过一样

这是我的密码:

public static void removeCard(String[] s) {
    Set<String> used;
    used = prefs.getStringSet("key1", null);

    for (String card : s) {
        if (card != null)
            used.remove(card);
        else 
            break;
    }

    editor.putStringSet("key1", used);
    editor.commit();
    Intent k = new Intent(cont, main3.class);
    cont.startActivity(k);
}
这就是我获取
SharedReferences
编辑器的方法

public static SharedPreferences prefs;
public static SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    cont = this;
    listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(new CustomAdapter(main2.this, mDrawableName, mDrawableImg, n));
    prefs = getSharedPreferences("MyPrefs", 0);
    editor = prefs.edit();
}
此外,要显示我使用的其他活动中的卡片,请执行以下操作:

SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
Set<String> cards = pref.getStringSet("key1", null);
tv1.setText("Available cards: " + cards);
SharedReferences pref=getSharedReferences(“MyPrefs”,Context.MODE\u PRIVATE);
Set cards=pref.getStringSet(“键1”,空);
tv1.setText(“可用卡:+卡”);

请添加您是如何获得SharedReference和editor@MatiasElorriaga当然,在函数存在的第一个活动中?@MatiasElorriaga添加了“我的朋友”。@DAVIDBALAS1检查您的编辑器。通过布尔成功=编辑器。提交()成功执行提交();也许有什么不对劲there@Pooya返回true:(
SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
Set<String> cards = pref.getStringSet("key1", null);
tv1.setText("Available cards: " + cards);