Crash upon calling getSharedPreferences()

Put your problem here if it does not fit any of the other categories.

Crash upon calling getSharedPreferences()

Postby Terazilla » Tue Feb 02, 2010 5:35 am

Has anyone had problems retrieving their shared preferences? I was under the impression that it created an empty one if no current prefs were saved, but I'm getting a crash instead. Basically, I've got a section in my default activity's constructor that reads like this:

SharedPreferences prefs = getSharedPreferences( "MyPrefsName", 0 );
onSharedPreferenceChanged( prefs, "MyPrefsName" );
prefs.registerOnSharedPreferenceChangeListener(this);

It doesn't even get as far as onSharedPreferenceChanged, instead I get a crash in some non-visible code immediately upon hitting getSharedPreferences! All the examples I've found online are doing the same thing I am, I think. The callstack says it's caused by a null pointer exception?

If I commented out getSharedPreferences the app launches just fine, and I can launch my PreferencesActivity screen, change some options, and back out no problem. =(

My previous project was a Live Wallpaper and doing the above seemed to work just fine. Can anybody help?
Terazilla
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Feb 02, 2010 5:26 am

Top

Re: Crash upon calling getSharedPreferences()

Postby bingxuewangzi427 » Wed Feb 03, 2010 11:29 am

Terazilla wrote:Has anyone had problems retrieving their shared preferences? I was under the impression that it created an empty one if no current prefs were saved, but I'm getting a crash instead. Basically, I've got a section in my default activity's constructor that reads like this:

SharedPreferences prefs = getSharedPreferences( "MyPrefsName", 0 );
onSharedPreferenceChanged( prefs, "MyPrefsName" );
prefs.registerOnSharedPreferenceChangeListener(this);

It doesn't even get as far as onSharedPreferenceChanged, instead I get a crash in some non-visible code immediately upon hitting getSharedPreferences! All the examples I've found online are doing the same thing I am, I think. The callstack says it's caused by a null pointer exception?

If I commented out getSharedPreferences the app launches just fine, and I can launch my PreferencesActivity screen, change some options, and back out no problem. =(

My previous project was a Live Wallpaper and doing the above seemed to work just fine. Can anybody help?


I don't know why it's crash,but I can give you my codes,you can try it~

put:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(........);
editor.commit();
get:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
beanbool bl= sp.getBoolean(............);
bingxuewangzi427
Developer
Developer
 
Posts: 38
Joined: Tue Jan 26, 2010 11:33 am

Postby Terazilla » Thu Feb 04, 2010 12:19 pm

After a bit of hair-pulling I did track this down -- you can't call getSharedPreferences in the activity's constructor, you need to wait until onCreate(). Otherwise things aren't done initializing and you get a null pointer exception.
Terazilla
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Feb 02, 2010 5:26 am

Re: Crash upon calling getSharedPreferences()

Postby theblitz » Tue Mar 29, 2011 1:01 pm

Thanks for this.
Just ran into the same problem and this solved it.
theblitz
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 18, 2011 9:11 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests