Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7669889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:41:50+00:00 2026-05-31T15:41:50+00:00

This is my first post on here so go easy on me lol! Ok

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:41:52+00:00Added an answer on May 31, 2026 at 3:41 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here and I wanted to get some input from
My first post here! Seems like this is the place to get wise ;)
This is my first post here. I have a problem. I need to take
this is my first post here on stackoverflow and am very impressed by the
Well, this is my first post here and really enjoying the site. I have
My first post here, so i hope this is the right area. I am
This is my first time here so I hope I post this question at
first post here at this great website! I would like to reduce the amount
my first post here. I am not much of a programmer, I can tailor
This is my first post here, therefore apologize for any blunders. I'm developing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.