Search code examples
javaandroideclipsepreferencesshared

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.

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. The Toast in the second activity displays the integer that I want. 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!


Solution

  • 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();