安卓:强制关闭后,共享preference错误 [英] Android: SharedPreference errors after force close

查看:331
本文介绍了安卓:强制关闭后,共享preference错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一些奇怪的效果后,我强行关闭我的应用程序。当应用程序与完成()关闭,一切都很好。我保存在共享preferences所以当应用程序被再次加载,它可以将这些变量恢复到UI一些变量。但是,如果我强行关闭应用程序,然后尝试继续在那里它已经离开,一些变量开始演技搞笑。我的意思是(在的onCreate)我检查,看看是否一个字符串,从共享preferences装,等于一个值(嘎吱嘎吱下来版):

I'm getting some weird effects after I force close my app. When the app is closed with finish(), everything is fine. I have some variables saved in a sharedPreferences so when the app is loaded again, it can restore those variables into the UI. However, if I force close the app and THEN try to continue where it had left off, some variables start "acting funny". By that I mean (in onCreate) I check to see if a string, loaded from the sharedPreferences, equals a value (crunched down version):

String namec;
private static final String TAG = "MyActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //namec was set as "forest" in a previous activity
    //which is bypassed if the user selects continue
    //from the main menu
    SharedPreferences pathtaken = getSharedPreferences("pathtakenpref", MODE_WORLD_READABLE);
    namec = pathtaken.getString("namec", "Unknown");

    ImageView v1 = (ImageView) findViewById(R.id.pathpic1);
    RelativeLayout v2 = (RelativeLayout) findViewById(R.id.pathmain);

    Log.i(TAG, "namec= " + namec);
    if(namec == "forest"){
        v1.setImageResource(R.drawable.forest);
        v2.setBackgroundResource(R.drawable.forestrepeat);
    }

}

在这里会发生什么事是namec,做实际上等于森林。我值发送到日志,它显示了变量,正是因为它应该是(森林)。然而,它不会运行里面的,如果{}的code。它给我的噩梦。我一直停留在这一个星期!

What happens here is namec, does in fact, equal "forest". I send the value to the log and it shows the variable exactly as it should be ("forest"). Yet it won't run the code inside of the if{}. It's giving me nightmares. I've been stuck on this for a week!

在同一code,我打开一组不同的共享preferences的(标记为TRH preFS)和那些每一个(6整数和3串)加载和显示就好了。我甚至增加一个,如果{}测试1串并从TRH preFS 1整数...他们都回来了真。

In the same code, I load a different set of sharedPreferences (labeled as TRHprefs) and each one of those (6 integers and 3 strings) load up and display just fine. I even add an if{} to test 1 string and 1 integer from TRHprefs... they both came back true.

问题1:有什么可以引起我的共享preferences XML成为,不知何故,损坏一个强制关闭

Q.1: Is there anything that can cause my sharedPreferences xml to become, somehow, corrupted on a force close?

问题2:有没有为我之前和之后我用力贴近帮助调试情况查看XML文件的方法。非常感谢!

Q.2: Is there a way for me to view the xml file before and after I use force close to help debug the situation. Thanks so much!

推荐答案

它是一个字符串。试试这个:

Its a String. Try this:

if("forest".equals(namec)){

这篇关于安卓:强制关闭后,共享preference错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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