Вопрос

I tried make my custom session, but I have a normal problem, but I don't see the error :

In my class Main :

session.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

Also in my Class session have :

public SharedPreferences getSharedPreferences(String mypreferences2, int modePrivate){
    sharedpreferences = getSharedPreferences(mypreferences2,
            modePrivate);
    return this.sharedpreferences;
}

And I get, why ?

03-12 00:22:09.104: E/AndroidRuntime(7595): FATAL EXCEPTION: main
03-12 00:22:09.104: E/AndroidRuntime(7595): java.lang.StackOverflowError
03-12 00:22:09.104: E/AndroidRuntime(7595):     at com.example.support.Session.getSharedPreferences(Session.java:41)
03-12 00:22:09.104: E/AndroidRuntime(7595):     at com.example.support.Session.getSharedPreferences(Session.java:41)
03-12 00:22:09.104: E/AndroidRuntime(7595):     at com.example.support.Session.getSharedPreferences(Session.java:41)
03-12 00:22:09.104: E/AndroidRuntime(7595):     at com.example.support.Session.getSharedPreferences(Session.java:41)

etc.

Это было полезно?

Решение

The reason you're getting a StackOverflowError is because your method is recursively calling itself ad infinitum. If your class Main is a subclass of Activity, you merely need to call:

this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

If, indeed, you do need information from Shared Preferences in another class, it would be advisable to retrieve it in your Activity and then pass the info to the class through a constructor, a public method, etc.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top