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 8688585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:24:20+00:00 2026-06-12T23:24:20+00:00

Hello and thanks for reading. I am trying to take an OOP approach to

  • 0

Hello and thanks for reading.

I am trying to take an OOP approach to using SharedPreferences to save and retrieve data in the android app I am working on. I believe the following code is correct as it works in the java classes when used directly in a non-OOP manor. However, in this SharedPref class I made, I am getting an error in Eclipse at the MODE_PRIVATE and I can’t figure out why. Thanks.

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class SharedPref {

    public static String File = "DPFile";

    public static void saveToSP(String key, String value) {
        SharedPreferences saveData = getSharedPreferences(File, MODE_PRIVATE);
        SharedPreferences.Editor editor = saveData.edit();
        editor.putString(key, value);
        editor.commit();
    }


    public static String getSavedData(String key) {
         SharedPreferences preferences = getSharedPreferences(File, MODE_PRIVATE);
         return preferences.getString(key, null);
    }
}

Additionally, if I extend the Activity class getSharedPreferences becomes the line with the error and the following message:

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

  • 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-06-12T23:24:24+00:00Added an answer on June 12, 2026 at 11:24 pm

    Probably the easiest way to fix this is to pass a Context into your two methods, and have it look something like this:

    public static void saveToSP(Context context, String key, String value) {
        SharedPreferences saveData = context.getSharedPreferences(File, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = saveData.edit();
        editor.putString(key, value);
        editor.commit();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello and thanks to everyone for reading my question. I've been working on a
Hello all and thanks for reading, Im trying to make a simple wave animation
Thanks for reading! var data = <html><head><title>Hello</title></head><body>Hello Body</body></html>; I want to print data including
Hello All you Lovely People on stackoverflow, I am trying to plot data using
Hello everyone and thanks for reading... I'm a Mono / Web developer and was
Hello Android programmers, I was trying to make a screen that has an EditText
Hello folks and thanks in advance for reading this. I currently use a slider
Hello everyone and thanks in advance for reading this.. I have the following html
Hello all and thanks for your time reading this. I need to verify certificates
Hello, thanks for reading my question. I have a string that I need to

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.