简体   繁体   English

Android SharedPreferences强制关闭应用

[英]Android SharedPreferences force closes app

This is my first post on here so go easy on me lol! 这是我在这里的第一篇文章,所以对我轻松一点! Ok so I'm new to this and I've been working on this code for days and I can't seem to get this concept of Preferences. 好的,所以我对此并不陌生,并且我已经在这段代码上工作了好几天,而且似乎无法理解“首选项”的概念。 I've searched everywhere on this site and I believe this code should work fine by all of the information I've looked at on this site and others. 我已经在该站点上到处搜索过,并且我相信我在该站点和其他站点上查看的所有信息都可以使此代码正常工作。 I've looked at countless examples and still don't understand what I'm doing wrong. 我看过无数的示例,但仍然不明白我在做什么错。

This is a snippet of my main activity that's first initiated when the user launches the app. 这是我的主要活动的摘要,该活动在用户启动应用程序时首次启动。 I have another activity on an options menu that calculates the difference between the current date and the user's selected date and I would like the resulting integer to be passed onto the main activity and show a Toast of it's value. 我在选项菜单上有另一个活动,该活动可计算当前日期与用户选择的日期之间的差,我希望将所得的整数传递到主活动上并显示其值的吐司。

public class SmokeStopperActivity extends Activity 
{   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        public static final String PREFERENCE_FILENAME = "DaysPassed"; 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   

        SharedPreferences preference = getSharedPreferences("DaysPassed", MODE_PRIVATE);
        int diffDays = preference.getInt("daysPassed", 0);
        Toast.makeText(SmokeStopperActivity.this, ("Days" + diffDays),
                Toast.LENGTH_LONG).show();;

This is a snippet of my second activity that calculates the value of the integer diffDays. 这是我的第二个活动的摘要,该活动计算整数diffDays的值。

long diff = milis2 - milis1;
      int diffDays = (int) (diff / (24 * 60 * 60 * 1000) + 30);


 Toast.makeText(SetDate.this, (diffDays),
         Toast.LENGTH_LONG).show();;

    SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);  
    SharedPreferences.Editor prefEditor1 = preference.edit();                                
    prefEditor1.putInt("daysPassed", diffDays);       
    prefEditor1.commit(); 

I have my second activity send a Toast of the diffDays integer when the user presses a button in a earlier section of the second activity and the calculations work fine. 当用户在第二个活动的较早部分中按下按钮时,我的第二个活动将发送diffDays整数的Toast,并且计算工作正常。 The Toast in the second activity displays the integer that I want. 第二个活动中的Toast显示我想要的整数。 The problem is that when I use this code 问题是当我使用此代码时

SharedPreferences preference = getSharedPreferences("DaysPassed", MODE_PRIVATE);
    int diffDays = preference.getInt("daysPassed", 0);
    Toast.makeText(SmokeStopperActivity.this, ("Days" + diffDays),
            Toast.LENGTH_LONG).show();;

in my first activity it force closes on open. 在我的第一个活动中,它强制打开。 If i delete this code from the first activity the app opens which doesn't make any sense to me. 如果我从第一次活动中删除此代码,则该应用会打开,这对我来说没有任何意义。 All of the other codes I have checked on here seem to use this snippet exactly as I do with no problems so I do not understand what I'm doing wrong. 我在此处检查过的所有其他代码似乎都完全按照我的方式正确使用了此代码段,因此我不理解自己在做什么错。 Any help would be GREATLY appreciated. 任何帮助将不胜感激。 I have a feeling it's something stupid that I keep overlooking. 我感觉自己一直在愚蠢地忽略自己。 Probably due from looking at code for hours upon hours lol! 可能是由于长时间查看代码而导致的大声笑!

我认为。您应该在活动的上下文中获得共享的偏好。

SharedPreferences prefs = this.getSharedPreferences( "DaysPassed", MODE_PRIVATE);

try this: 尝试这个:

SharedPreferences preference = SmokeStopperActivity.this.getSharedPreferences("DaysPassed", MODE_PRIVATE);
    int diffDays = preference.getInt("daysPassed", 0);
    Toast.makeText(SmokeStopperActivity.this, ("Days" + String.valueOf(diffDays)),
            Toast.LENGTH_LONG).show();

Use this 用这个

Context context = getApplicationContext();
Toast.makeText(context, ("Days" + diffDays),
            Toast.LENGTH_LONG).show();

The problem is that when I use this code 问题是当我使用此代码时

SharedPreferences preference = getSharedPreferences("DaysPassed", MODE_PRIVATE);
int diffDays = preference.getInt("daysPassed", 0);
Toast.makeText(SmokeStopperActivity.this, ("Days" + diffDays),
        Toast.LENGTH_LONG).show();;

in my first activity it force closes on open. 在我的第一个活动中,它强制打开。 If i delete this code from the first activity the app opens which doesn't make any sense to me. 如果我从第一次活动中删除此代码,则该应用会打开,这对我来说没有任何意义。

Correct. 正确。 It makes no sense at all. 这根本没有意义。 There is nothing wrong with that code...well, actually there is. 该代码没有错...嗯,实际上是。 I'd write it as... 我会写成...

Toast.makeText(this, "Days" + diffDays, Toast.LENGTH_LONG).show();

...but either way, it works - my version and yours. ...但是无论哪种方式,它都有效-我的版本和您的版本。 There is something else wrong with your code and it isn't to do with those three lines. 您的代码还有其他问题,与这三行代码无关。

Forget your second Activity and just post the whole of your first Activity and also the logcat output indicating which line is throwing an unhandled exception and causing the force close. 忘记第二个Activity ,只发布整个第一个Activity ,并显示logcat输出,该输出指示哪一行抛出未处理的异常并导致强制关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM