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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:49:01+00:00 2026-06-02T01:49:01+00:00

I made a SharedPreferences class so that the rest of my Activitys could use

  • 0

I made a SharedPreferences class so that the rest of my Activitys could use on line to get preferences.

 public class SPAdapter extends Activity {

    public final String preferenceSettings = "STORAGE";

        // Default value if row does not exist in preference
     public static final String rowExistence = "Row did not exist";

public String prefGet(String preferenceName, String rowId) {
    SharedPreferences preferenceObject = getSharedPreferences(preferenceName, MODE_PRIVATE);
    String value = preferenceObject.getString(rowId, rowExistence);
    return value;
}

I use this method like so:

    public class Splash extends Activity {

     private SPAdapter spObject;
     public String rowNumber(0);
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);

         spObject.prefGet(spObject.preferenceSettings,
            rowNumber);
}

However, whenever I try to use this method, the application crashes showing an error about context. Can anybody give me a hand getting this to work?

  • 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-02T01:49:03+00:00Added an answer on June 2, 2026 at 1:49 am

    The problem with this approach is that you extend SPAdapter from Activity and don’t initialize it properly, it doesn’t have a proper Context set, so it can’t get a SharedPreferences instance from the framework through it. But anyways, this is not the way you should do things in Android, you’re not the one who instantiates new Activity objects, it’s supposed to be the framework’s job. Here’s 2 ways to get this working:

    • Extend your Splash activity from SPAdapter.

    • Don’t extend SPAdapter from Activity, just pass a reference for a Context object and get the SharedPreferences instance using that. Something like this:

      public class SPAdapter {
      public final String preferenceSettings = “STORAGE”;

      // Default value if row does not exist in preference
      public static final String rowExistence = "Row did not exist";
      
      private Context ctx;
      
      public SPAdapter(Context ctx){
          this.ctx = ctx;
      }
      
      public String prefGet(String preferenceName, String rowId) {
          SharedPreferences preferenceObject = ctx.getSharedPreferences(
                  preferenceName, Context.MODE_PRIVATE);
          String value = preferenceObject.getString(rowId, rowExistence);
          return value;
      }
      

      }

    Use it like this:

    public class FsActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            SPAdapter sa = new SPAdapter(getApplicationContext());
            sa.prefGet("", "");
        }
    }
    

    The first solution seems a bit better imo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I have a class that extends PreferenceActivity and shows the preference screen of my
I made a class for DB connection like this: public class DbHelper : IDisposable
I have made a shared preference activity that store the user settings, now i
I'm trying to get the call I just made/received. For that I'm using BroadcastReceiver
I made a custom control that is basically a multiline TextBox that allows input,
I`v made my webpart that works well (I can add it to page and
I do wallpapers to andoid and I want that the user could choose options.
Made a fiddle for this: http://jsfiddle.net/terjeto/MN4FJ/ My problem is that dragleave fires when you
made a short java program in Bluej upon compiling it and trying to get

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.