本文介绍了设置<字符串>在Android的共享preferences不保存强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去使用共享preferences机器人,从来就记录了一切,低于code真的犯字符串集。问题是,当我强行关闭应用程序,并重新启动,则settings.getStringSet返回一个空集。没有errormessages任何地方。

从来就试图preferenceManager.getDefaultShared preferences但对我来说,要么不工作。

感谢您的时间。

 公共静态最后弦乐preFS_NAME =我的prefsFile;
私有静态最后弦乐FOLLOWED_ROUTES =followedRoutes;
 

和后来当保存被称为:

 公共无效onFollowClicked(查看视图){

共享preferences设置= getShared preferences(preFS_NAME,MODE_PRIVATE);
共享preferences.Editor编辑器= settings.edit();

设置<字符串>如下= settings.getStringSet(FOLLOWED_ROUTES,新的HashSet<字符串>());
follows.add(routeId);

editor.putStringSet(FOLLOWED_ROUTES,如下);
editor.commit();

}
 

解决方案

看看<一href="http://developer.android.com/reference/android/content/Shared$p$pferences.html#getStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29">here.

也为REFFERENCE:

<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.html">Shared$p$pferences

<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.Editor.html">Shared$p$pferences.Editor

编辑:

实际上有与这一个错误,请参阅这里。从那里摘录:

EDIT2:

有可能是一个溶液<一href="http://stackoverflow.com/questions/7361627/how-can-write-$c$c-to-make-shared$p$pferences-for-array-in-android">here,一起来看看。

Im trying to use androids sharedpreferences, I´ve logged everything and the code below really commits the string set. The problem is when I force close the app and start again, the settings.getStringSet returns an empty set. No errormessages anywhere.

I´ve tried PreferenceManager.getDefaultSharedPreferences but that does not work for me either.

Thanks for you time.

public static final String PREFS_NAME = "MyPrefsFile";
private static final String FOLLOWED_ROUTES = "followedRoutes";

and later on when saved is called:

public void onFollowClicked(View view){

SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();

Set<String> follows =  settings.getStringSet(FOLLOWED_ROUTES, new HashSet<String>());
follows.add(routeId);

editor.putStringSet(FOLLOWED_ROUTES, follows);
editor.commit();

}
解决方案

Take a look here.

Also for refference:

SharedPreferences

SharedPreferences.Editor

EDIT:

There's actually a bug with this one, see here. An extract from there:

EDIT2:

There might be a solution here, take a look.

这篇关于设置&LT;字符串&GT;在Android的共享preferences不保存强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:26