Tushar Monirul
Tushar Monirul

Reputation: 5064

How to get getSharedPreferences in android?

I have two activities(MainActivity and Player). In MainActivity I have created two methods:

private final long DEFAULTLONG = 0;
public long getTotalPlayTime(){
        SharedPreferences sp = getApplicationContext().getSharedPreferences("TotalPlaybackTime", MODE_PRIVATE);
        long tempTotalTime = sp.getLong("totalPlayTime", DEFAULTLONG);
        return tempTotalTime;
}

public void updateTotalPlayTime(){
        SharedPreferences sp = getSharedPreferences("TotalPlaybackTime", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong("totalPlayTime", totalPlayTime);
        editor.commit();
 }

And when I am calling these two methods from Player activity, my app is crashing. I have instantiated the MainActivity in Player and called those methods by this line:

if (preferenceScheduler++ == 10) {
      f.updateTotalPlayTime();
      seconds2 = (f.getTotalPlayTime()) % 60;
      minutes2 = ((f.getTotalPlayTime() - seconds2)) / 60;
}

I don't know why, its gives me NullPointerException.

Here are the full error log:

java.lang.NullPointerException
            at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
            at tushar.prime.primeplayer.MainActivity.updateTotalPlayTime(MainActivity.java:329)
            at tushar.prime.primeplayer.player.update(player.java:506)
            at tushar.prime.primeplayer.player.access$000(player.java:40)
            at tushar.prime.primeplayer.player$1.run(player.java:133)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 342

Answers (2)

Marcus
Marcus

Reputation: 6717

getSharedPreferences() can only be called after onCreate() has been called on an Activity.

I would change your updateTotalPlaytime() method to

public void updateTotalPlayTime(Context ctx, String value){
        SharedPreferences sp = ctx.getSharedPreferences("TotalPlaybackTime", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong("totalPlayTime", value);
        editor.commit();
 }

And call it like this, in your Activity (after onCreate);

updateTotalPlaytimg(this, "value");

EDIT:

You should not instantiate your MainActivity. Instead, create the updateTotalPlaytime(); method in your PlayerActivity.

Upvotes: 1

I think people should use SharedPref with singleton because you can access every where you want for example i use it is like:

SharedPreferences preferences;
Editor editor;

public SharedPreference(Activity activity) {
    preferences = activity.getSharedPreferences(Const.XMLFILEPATH, Activity.MODE_PRIVATE);
    editor = preferences.edit();
}

public static SharedPreference getInstance(Activity activity){
    if(sharedPreference==null)
        sharedPreference = new SharedPreference(activity);
    return sharedPreference;
}

and when i want to edit or update sharedPref

public void isRootControlled(boolean isControlled){ 
    try {
        editor.putBoolean(Const.IS_ROT_CONTROLED, true); 
        editor.commit();
    } catch (Exception e) {} 
}

public boolean isRootControlled(){
    return preferences.getBoolean(Const.IS_ROT_CONTROLED, false);
}

Upvotes: 0

Related Questions