Shared Preferences Help Needed

Last Updated:

  1. TheAngelsCry

    TheAngelsCry New Member

    Okay, I've been charged with creating an SMS application for my College Project, however, I've run into a few difficulties.

    I'm able to store text in a shared preferences in my, and the information recalls perfectly when the program is paused and then restarted - so I know that code works.

    My problem is that when I want to display this information AGAIN in a different file ( it's not working. Any ideas what I'm doing wrong?

    Any help would really be appreciated! ^_^

  2. markb

    markb Well-Known Member


    Hopefully I'm not missing something obvious, but in you don't seem to be assigning anything to your prefsInitial variable.

    Unless there's more code that you haven't posted, I'd guess that could be your problem.

    This line in should be giving you a null pointer exception
    Code (Text):
    2. String check = prefsInitial.getString("initial","");

  3. TheAngelsCry

    TheAngelsCry New Member

    I'm not receiving any error within Eclipse itself. The only error I get is when I try and run The application Fault Reporter (process sms.project) has stopped unexpectedly. Please try again.

    Plus, that line of code works when it's placed within , but when I try and call the same information in a different class, it's not working.

    Or am I missing your point completely?


    Okay, I've realised I'm using an Activity Preference and not a Shared Preference... but these online tutorials are really quite confusing. Can anyone simplify what needs to be done for a Shared Preference to work?
  4. markb

    markb Well-Known Member

    This line will cause a crash in because prefsInitial hasn't been given a value yet, so it will be null.

    Code (Text):
    2. String check = prefsInitial.getString("initial","");
    If you copy that line somewhere else, where prefsInitial HAS been set and is not null, then it won't crash. But that's pretty irrelevant, as it's the crash in you need to fix. In you need to set up the prefsInitial variable before you use it.

    You said:
    Code (Text):
    2. The only error I get is when I try and run
    3. The application Fault Reporter (process sms.project) has stopped unexpectedly
    That will be because of the null pointer exception, caused by using prefsInitial when it has a null value.

    I suggest looking at the logs to see error messages and the exception traceback, and then you'll see where it is crashing.

  5. SFLeBrun

    SFLeBrun Member

    The first thing that I noticed with the code in your post is that you are not using the getSharedPreferences() API that I know in the first code and never call it in the second Send class. The second part has been mentioned in previous replies so I will not go into that.

    The API that I have used is:
    Code (Text):
    1. Context.getSharedPreferences( String name, int mode );
    Your call is missing the Preference (a.k.a. properties file) name. I am assuming that the call you are using is using a default Preference file. Preferences that are shared within a single application are named (file name). Check to see if there are other classes that implement the getSharedPreferences() interface.

    Check out Data Storage | Android Developers for more details.

    In one of your replies, you mentioned that the two Activiy classes run as separate applications. The SharedPreferences are not visible between two different applications unless both applications use the same "Shared User ID". This could also be your problem if you are running the two Activity classes in different applications.

    You may also want to consider a ContentProvider to handle persistent data shared between your multiple SMS applications. This is more complex that using SharedPreferences so which one is better for your needs depend on how much and what type of data you want to share.
  6. SFLeBrun

    SFLeBrun Member

    The code from your Setting Class uses the private to an Activity Preferences.
    Code (Text):
    2. ...
    3. public SharedPreferences prefsInitial;
    4.     ...
    5.         prefsInitial = getPreferences(MODE_WORLD_READABLE);
    6.         ...
    If you want to share preferences between components of the same application the SharedPreference interface:

    Code (Text):
    2. prefsInitial = getContext().getSharedPreferences("MySettings", MODE_WORLD_READABLE);
    Even though the getPreferences() returns a SharedPreferences object, the data stored in it is only accessible to that one Activity in that one Application.
  7. Deepa

    Deepa New Member

    How to use shared preference between two applications
  8. kpbird

    kpbird Member

  9. KrishRud

    KrishRud New Member

    KP Bird has a nice artical and lot of detailed explanation. I like it.

    To answer Deepa's question :

    Use MODE_WORLD_READBLE in the MODE property while you are creating the Shared Preference Vairable. If you want a to the point artical you can check here.

Share This Page