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

  • SEARCH
  • Home
  • 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 1116045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:14:43+00:00 2026-05-17T03:14:43+00:00

I have some information stored as SharedPreferences. I need to access that information from

  • 0

I have some information stored as SharedPreferences. I need to access that information from outsite an Activity (in from a domain model class). So I created a static method in an Activity which I only use to get the shared preferences.

This is giving me some problems, since apparently it is not possible to call the method “getSharedPreferences” from a static method.

Here’s the message eclipse is giving me:

Cannot make a static reference to the non-static method 
getSharedPreferences(String, int) from the type ContextWrapper

I tried to work around this by using an Activity instance, like this:

public static SharedPreferences getSharedPreferences () {
  Activity act = new Activity();
  return act.getSharedPreferences("FILE", 0);
}

This code gives a null point exception.

Is there a work-around? Am I going into an android-code-smell by trying to do this?

Thanks in advance.

  • 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-17T03:14:44+00:00Added an answer on May 17, 2026 at 3:14 am

    That’s because in this case, act is an object that you just create. You have to let Android do that for you; getSharedPreferences() is a method of Context, (Activity, Service and other classes extends from Context). So, you have to make your choice:

    • If the method is inside an activity or other kind of context:

      getApplicationContext().getSharedPreferences("foo", 0);
      
    • If the method is outside an activity or other kind of context:

      // you have to pass the context to it. In your case:
      // this is inside a public class
      public static SharedPreferences getSharedPreferences (Context ctxt) {
         return ctxt.getSharedPreferences("FILE", 0);
      }
      
      // and, this is in your activity
      YourClass.this.getSharedPreferences(YourClass.this.getApplicationContext());
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have some information about the scoping in JavaScript. I know that
I have some WCF methods that are used to transmit information from a server
I have some information stored in an xml file. I need to display the
I have C# application that must store some information into MS SQL that would
I have some information in my database like 'author', 'book' etc., that are all
I have to develop an application that display some information about the possessor of
I have several gen_server workers periodically requesting some information from hardware sensors. Sensors may
I'm having some trouble restored SharedPreferences into an activity separate from the original. I
I have a fairly simple web app that uses cookies to store some information
I have some user-specific data that I need to store in SharePoint and make

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.