1

getSharedPreferences is creating problem for me i have searched stackoverflow other posts but the solution given is not working for me as expected

here is my code

    public class checksystem extends Activity {
        static SharedPreferences settings ;
            static SharedPreferences.Editor editor;
            public static final String PREFS_NAME = "file";

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        settings = getSharedPreferences(PREFS_NAME, 0);
                editor = settings.edit();
                editor.commit();

                 checksystem.ss();
}
    ....

and error is

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

now actually i want to access file.xml prefs to save some work from this static method but i am unable to save due to this error

  • possible duplicate of [Having a difficult time making a static method which utilizes non-static methods](http://stackoverflow.com/questions/11321853/having-a-difficult-time-making-a-static-method-which-utilizes-non-static-methods) – BradleyIW May 07 '15 at 17:18
  • Take a look at http://stackoverflow.com/questions/28986384/how-to-display-sharedpreference-stored-data-in-fragment/28987464#28987464. – Dhaval Patel May 07 '15 at 17:23
  • 1
    Why do you have a `public static void main(String[] args) {` inside an `Activity` of an android project ? – Blusky May 07 '15 at 17:32
  • Dhaval i tried your class but somehow im getting problem and maybe due to context ,i used this" static Context mContext;" is it okay ? – androidstarter May 07 '15 at 17:35
  • @androidstarter If you want to use static Context -> you can use context of AndroidApplication. (It's mean you need create class Application then use context of Application. For create Application class you can search on GG). For your example it's not like create android app. Android Activity have it's lifecycle. It's not start with "static main" method. Please read android docs. For your error -> ofcource. It's error by java compile. Static method can't referent non-static variable. If you are starter I think don't use static too much. (after you have exp you can use it). Hope this help – quangson91 May 07 '15 at 17:42
  • Blusky used oncreate still problem – androidstarter May 07 '15 at 17:46

0 Answers0