Stu wrote:
> Hi, I seem to be struggling with the concept of Context in Android
> when dealing with the application preferences.
> 
> I have an application with a few different classes. I need to be able
> to get the value of one of the preferences from a class that is not my
> main class, but I can't seem to get it working at all.
> 
> I get a NullPointerException with the following (as well as about a
> hundred other variations):
> 
>     final SharedPreferences prefs =
> PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
>     final boolean increasing = prefs.getBoolean("preference_name",
> false);
> 
> Am I doing something completely stupid? 

Activity is a Context.

Service is a Context.

Anything on which you can call getApplicationContext() is a Context.

Hence, get rid of your call to getApplicationContext() and replace it
with "this" (minus the quotes).

Only use getApplicationContext() when you specifically and absolutely
need an Application object.

> Is there an example app
> that anyone can think of that might be helpful? 

For SharedPreferences, try:

http://github.com/commonsguy/cw-android/tree/master/Prefs/Dialogs/

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 3.0
Available!

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to