麻烦的应用程序运行过程中共享preferences冲突的,然后全部设备的应用程序强制关闭/重新启动后消失 [英] Trouble with SharedPreferences conflicting during app operation and then disappearing entirely after an app force close/reboot of device

查看:199
本文介绍了麻烦的应用程序运行过程中共享preferences冲突的,然后全部设备的应用程序强制关闭/重新启动后消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示我的问题是,一些共享preferences我使用的是矛盾而应用程序操作,例如他们似乎只是在写每一个接管价值,而不是用他们的钥匙。

As the title suggest my problem is that some SharedPreferences I'm using are conflicting while the app operates, for example they seem to just be over writing each overs values, and not using their 'key'.

此外,一旦应用程序关闭完全,强制关闭/重新启动,该文件将完全丧失。

Additionally once the application closes fully, force close/reboot, the file is lost completely.

LogCat中显示了这些可能的暗示,但我不能为我的生命得到任何工作:

Logcat shows these possible hints, but I can't for the life of me get anything to work:

07-22 13:28:13.980: W/SharedPreferencesImpl(7905):org.xmlpull.v1.XmlPullParserException: Map value without name attribute: string
and
07-22 13:28:13.980: W/SharedPreferencesImpl(7905):  at android.app.SharedPreferencesImpl.loadFromDiskLocked(SharedPreferencesImpl.java:113)

下面是我的code为共享preFS:

Here is my code for the SharedPrefs:

public class SharedPrefs 
{
public static String PREF_FILE = "HangedRes";
public static String USERSCORE;
public static String EXTRAWORDS;

/////////////////////////////////////////////////////////////////

static SharedPreferences settings;
static SharedPreferences.Editor editor;

///////////////////////////////////////////////////////////////////

public static String getStringPreference(Context context, String key) 
{
    settings = context.getSharedPreferences(PREF_FILE, Context.MODE_WORLD_READABLE);

    editor = settings.edit();

    String result = settings.getString(key, null);

    return result;
}

public static void setStringPreference(Context context, String key, String value) 
{
    settings = context.getSharedPreferences(PREF_FILE, Context.MODE_WORLD_READABLE);
    editor = settings.edit();
    editor.putString(key, value);
    //editor.apply();
    editor.commit();
}

/*
public static boolean getBooleanPreference(Context context, String key) {
    settings = context.getSharedPreferences(PREF_FILE, 0);
    editor = settings.edit();

    boolean result = settings.getBoolean(key, false);
    return result;
}

public static void setBooleanPreference(Context context, String key, boolean value) 
{
    settings = context.getSharedPreferences(PREF_FILE, 0);
    editor = settings.edit();

    editor.putBoolean(key, value);
    editor.commit();
}
*/


}

这里是我如何调用我的主要code这些方法:

Here is how I am calling those methods in my main code:

 // Get current user points and display //////////////////////////
    userScore = SharedPrefs.getStringPreference(this, SharedPrefs.USERSCORE);

    //Check if things are unlocked
    ownWords = SharedPrefs.getStringPreference(this, SharedPrefs.EXTRAWORDS);

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

哦!

public static String USERSCORE;
public static String EXTRAWORDS;

在这里你初始化这些键..? :)

where you initialized these keys..?? :)

请它像这样

public static String USERSCORE="USERSCORE";
public static String EXTRAWORDS="EXTRAWORDS";`

这篇关于麻烦的应用程序运行过程中共享preferences冲突的,然后全部设备的应用程序强制关闭/重新启动后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆