You need to do this in onCreate() or later.  Service and Activity are not
fully initialized when the constructor is called and you generally shouldn't
put any code there.

On Wed, Sep 23, 2009 at 7:48 PM, brooke <brooke.ma...@gmail.com> wrote:

>
> I have some shared preferences (user_id, email) that I want to access
> from services and classes that are not subclassed from Activity.  I
> have been trying to implement this today and keep hitting roadblocks.
>
> In particular, when I try to access getSharedPreferences, I get a null
> pointer exception.  My code is posted below
>
> My goal here is to allow read access the shared preferences to objects
> and (potentially) services that aren't going to be directly exposed to
> the user.  Any suggestions/examples that can help me along?
>
>
> Many thanks,
>
> Brooke
>
> <snip>
> public class MyPrefs extends Service {
>    public static final String PREFS_NAME = "MyPrefs";
>    private int user_id;
>    private String user_email;
>    private String user_password;
>    private Editor editor = null;
>
>    private SharedPreferences settings = null;
>
>
>    public MyPrefs () {
>
>        settings = this.getSharedPreferences(PREFS_NAME,
> Context.MODE_PRIVATE);
>    }
>
> </snip>
>
> the this.getSharedPreferences line causes a null pointer exception.
>
>
> >
>


-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to