0

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!

user1275331
  • 23
  • 1
  • 2
  • 5

4 Answers4

0

I think..You should get shared preferences on context of your activity... try like this..

SharedPreferences prefs = this.getSharedPreferences( "DaysPassed", MODE_PRIVATE);
5hssba
  • 8,079
  • 2
  • 33
  • 35
  • That wont help. Using a 'String + Integer' construct when providing a String (or CharSequence) parameter will implicitly convert the Integer to a String during the concatenation process. – Squonk Mar 17 '12 at 06:21
  • yes...i faced same type of problem earlier.. but then i dint use concatenation.. instead i tried to make toast with int in it.. and it showed me error.. so then i used String.valueOf...thnx for correcting.. – 5hssba Mar 17 '12 at 06:39
0

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();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Why would you use `getBaseContext()` when passing a `Context` to `Toast.makeText(...)` within an `Activity`? All you have to do is use `this`. Also, using a 'String + Integer' construct when providing a String (or CharSequence) parameter will implicitly convert the Integer to a String during the concatenation process. There's no need to use `String.valueOf(...)`. – Squonk Mar 17 '12 at 06:23
  • @MisterSquonk :i agree with you.but it's depend on where we are using Toast.makeText(Context cont). see this [getBaseContext() instead of this](http://stackoverflow.com/questions/9605459/android-why-must-use-getbasecontext-instead-of-this/9606712#9606712) – ρяσѕρєя K Mar 17 '12 at 06:33
  • The question shows that `Toast.makeText(...)` is being called in the `onCreate(...)` method of an `Activity` - in this case there is no point in using anything other than `this`. – Squonk Mar 17 '12 at 06:48
0

Use this

Context context = getApplicationContext();
Toast.makeText(context, ("Days" + diffDays),
            Toast.LENGTH_LONG).show();
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
0

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.

Squonk
  • 48,735
  • 19
  • 103
  • 135