共享首选项不起作用

时间:2015-10-26 17:51:40

标签: android preferences shared

我遇到一个问题,我的共享首选项不能在类文件中工作。我很困惑,无法解决它.Below是我的文件globalfile,它保存数据如下。

public class globalfile extends Activity {
SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
public static final String Pwd = "pwdKey";
public static final String Email = "emailKey";

private static String global_username = "null/", global_pwd = "null/";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);

}

public String getusername() {

    global_username = sharedpreferences.getString(Email, "");
    return global_username;
}

public String getuserpwd() {

    global_pwd = sharedpreferences.getString(Pwd, "");
    return global_pwd;

}

public void setusername(String someVariable) {
    global_username = someVariable;
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(Email,global_username);
    editor.commit();

}

public void setuserpwd(String someVariable) {
    global_pwd = someVariable;
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(Pwd,global_pwd);
    editor.commit();

}
}

我首先调用了setuserpwd()& setusername()然后getuserpwd()& getusername()来自另一个使用类globalfile.But的对象的活动总是返回null.although如果我使用这个代码没有共享pref.it工作正常

2 个答案:

答案 0 :(得分:0)

create an instance of Activity with Activity class object

从技术上讲,您可以创建Activity类的实例。但是,活动实例将是无用的,因为它的底层Context不会被设置。

规则是你永远不应该自己创建Android组件的实例(Activity,Service,BroadcastReceiver,Provider)(使用new关键字或其他方法)。这些类只能由Android框架创建,因为Android为这些对象设置了底层Context,并且还管理了生命周期。

我认为你新创建的活动对象无法获得实际的上下文。所以尽量避免你当前的流量。我建议你创建一个可以用作工厂类的单独类,我认为你的问题将会解决。  或者另一种解决方案是这样的:

    Context context = /*get application context*/
    SharedPreferences sharedPref = context.getSharedPreferences(
      getString(R.string.preference_file_key), Context.MODE_PRIVATE);
    /*get value from this sharedPref*/

所以你不需要创建活动类对象,在这里你应该避免使用以前的getter方法来获取值。

答案 1 :(得分:0)

将globalfile更改为实用程序类而不是活动,因此应用程序的其他部分(非活动但可访问android上下文)可以非常轻松地使用它。

public class Global {
    private final SharedPreferences sharedpreferences;
    public static final String mypreference = "mypref";
    public static final String Pwd = "pwdKey";
    public static final String Email = "emailKey";

    private static String global_username = "null/", 
        global_pwd = "null/";

    public Global(Context context) {
        this.sharedpreferences = context.getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);
    }


    public String getusername() {

        global_username = sharedpreferences.getString(Email, "null/");
        return global_username;
    }

    public String getuserpwd() {

        global_pwd = sharedpreferences.getString(Pwd, "null/");
        return global_pwd;

    }

    public void setusername(String someVariable) {
        global_username = someVariable;
        sharedpreferences.edit().putString(Email,global_username).commit();
    }

    public void setuserpwd(String someVariable) {
        global_pwd = someVariable;
        sharedpreferences.edit().putString(Pwd,global_pwd).commit();

    }
}

以下是如何使用新的util类

Global global = new Global(context);
global.setusername("foo");
Log.d("TAG", "username from prefs = " + global.getusername());
global.setuserpwd("bar");
Log.d("TAG", "password from prefs = " + global.getusername());