Android:强制关闭后 SharedPreference 错误

发布于 2024-12-05 19:01:13 字数 1336 浏览 1 评论 0原文

强制关闭应用程序后,我收到一些奇怪的效果。当使用 finish() 关闭应用程序时,一切都很好。我在共享首选项中保存了一些变量,因此当再次加载应用程序时,它可以将这些变量恢复到 UI 中。但是,如果我强制关闭应用程序,然后尝试从中断处继续,某些变量就会开始“表现得很有趣”。我的意思是(在onCreate中)我检查从sharedPreferences加载的字符串是否等于一个值(简化版本):

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,实际上等于“forest”。我将值发送到日志,它完全按照应有的方式显示变量(“森林”)。但它不会运行 if{} 内的代码。它让我做噩梦。我已经被这个问题困扰了一个星期了!

在相同的代码中,我加载一组不同的共享首选项(标记为 TRHprefs),其中每个(6 个整数和 3 个字符串)加载并显示得很好。我什至添加了一个 if{} 来测试 TRHprefs 中的 1 个字符串和 1 个整数...它们都返回 true。

问题 1:是否有任何因素会导致我的 SharedPreferences xml 在强制关闭时以某种方式损坏?

Q.2:有没有办法让我在使用强制关闭之前和之后查看 xml 文件以帮助调试情况。非常感谢!

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

}

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!

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.

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-12-12 19:01:13

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

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

Its a String. Try this:

if("forest".equals(namec)){
我喜欢麦丽素 2024-12-12 19:01:13

如果你想比较两个字符串,你需要使用这个:

if(namec.equals("forest")){
        v1.setImageResource(R.drawable.forest);
        v2.setBackgroundResource(R.drawable.forestrepeat);
    }

If you want to compare two String you need to use this:

if(namec.equals("forest")){
        v1.setImageResource(R.drawable.forest);
        v2.setBackgroundResource(R.drawable.forestrepeat);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文