如何在android中获取getSharedPreferences?

时间:2015-02-09 12:38:21

标签: java android

我有两个活动(MainActivity和Player)。在MainActivity中,我创建了两个方法:

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();
 }

当我从播放器活动中调用这两个方法时,我的应用程序崩溃了。 我在Player中实例化了MainActivity并通过以下行调用了这些方法:

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

我不知道为什么,它给了我NullPointerException。

以下是完整的错误日志:

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)

2 个答案:

答案 0 :(得分:1)

只有在getSharedPreferences()上调用onCreate()后,才能调用{p> Activity

我会将您的updateTotalPlaytime()方法更改为

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();
 }

并在Activity(在onCreate之后)

之后调用它
updateTotalPlaytimg(this, "value");

编辑:

您不应该实例化MainActivity。而是在updateTotalPlaytime();中创建PlayerActivity方法。

答案 1 :(得分:0)

我认为人们应该将SharedPref与singleton一起使用,因为你可以访问你想要的每个地方,例如我使用它就像:

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;
}

当我想编辑或更新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);
}