如何从其他活动更新SharedPreference?

时间:2016-02-06 13:29:28

标签: android android-sharedpreferences

我已经在LogIn类中创建了SharedPreference,并且我在一个方法中添加了SavedPreference()

LogIn.java

public void SavePreferences(String key, String value){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString(key, value);
    editor.commit();

}

现在在我的其他活动中,我想通过调用方法更新它或将字符串添加到它,并在单击确定按钮后添加这些参数。

Menu.java

ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String oldpassword = oldpass.getText().toString();
                    String newpassword = newpass.getText().toString();
                    if (LogIn.s.toString().equals("1234")) {
                        if (oldpassword.toString().equals("1234")) {
                            logIn.SavePreferences("password",newpassword);
                            Toast.makeText(Menu.this,"New password saved!",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Toast.makeText(Menu.this,"Old Password is not correct",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else {
                        if (oldpassword.equals(LogIn.realpass)){
                            logIn.SavePreferences("password",newpassword);
                            Toast.makeText(Menu.this,"New password saved!",Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(Menu.this,"Old Password is not correct",Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            });

但点击确定后,我的应用程序崩溃了,LogCat显示了这个

02-06 13:21:55.834 6327-6327/com.secsys.gagacamaso.gagacamaso E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.secsys.gagacamaso.gagacamaso, PID: 6327
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                                at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
                                                                                at com.secsys.gagacamaso.gagacamaso.LogIn.SavePreferences(LogIn.java:191)
                                                                                at com.secsys.gagacamaso.gagacamaso.Menu$3$1.onClick(Menu.java:133)
                                                                                at android.view.View.performClick(View.java:5198)
                                                                                at android.view.View$PerformClick.run(View.java:21147)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

LogIn.java:191是SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

和Menu.java:133是logIn.SavePreferences("password",newpassword);

3 个答案:

答案 0 :(得分:3)

最好将context作为{{1>}传递给 SavePreferences

argument

答案 1 :(得分:0)

制作该方法static,因为这样您就可以从应用内的任何其他activity更新它。

答案 2 :(得分:0)

我建议你在Application类中编写函数SavePreferences(String key, String value)而不是任何活动,因为它可以在多个活动中使用,然后你可以轻松地用你的应用程序实例调用该函数,例如

public class MyApplication extends Application
{
    //your app constructors and other related code
    ....
    ....

    public void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString(key, value);
        editor.commit();

    }
    ....
    ....
}

public class MyActivity extends Activity{

    //your Activity code
    ....
    ....

    private void MyFunction(){
        //assuming you have a static getter for the application object
        MyApplication.getApplication().SavePreferences("password",password);

    }

}