Search code examples
javaandroidsharedpreferencesandroid-preferencespreferenceactivity

nullpointer on PreferenceManager.getDefaultSharedPreferences(getBaseContext());


I'm trying to fetch my data from inside a dataprovider, since the dataprovider doesn't provide a context I've created following function:

public String getUN() {
    Log.w("app", "getUN");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    Log.w("app", "getUN2");
    return prefs.getString("username", "");
}

I've used eclipse debugging (along with the Log messages) to find the point of the error: the preferencemanager-line

called from another class trough:

PreferencesActivity pa = new PreferencesActivity();
Log.w("app","user:"+pa.getUN());

(again Log for debugging)

Thanks.

edit: alternative method for fetching the data is also welcome, data is stored using android's 'native' method (via preference xml)


Solution

  • You should pass context to your data provider.