Hi Mark,

I have made the change you suggested, but that puts me back to the
error I was getting originally:

E/AndroidRuntime( 9948): Uncaught handler: thread main exiting due to
uncaught exception
[...]
E/AndroidRuntime( 9948): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 9948):        at
android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
E/AndroidRuntime( 9948):        at
android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:
356)
E/AndroidRuntime( 9948):        at
android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:
351)

which is caused by line 50 of my class: final SharedPreferences prefs
= PreferenceManager.getDefaultSharedPreferences(this);

This error message doesn't really help me very much... Everything
appears to be the same as in the Dialogs example you pasted a URL to.
Any more suggestions?

Regards,
Stuart.

On Apr 9, 5:01 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> 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