共享首选项不起作用
我有一个问题,我的共享首选项不工作在类文件中。我很困惑,无法解决它。以下是我的文件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()然后,使用类globalfile.But的对象总是返回null.although如果我使用这个代码,无需共享pref.it getuserpwd()& getusername()从另一个活动做工精致
create an instance of Activity with Activity class object
从技术上讲,你可以创建一个Activity类的一个实例。但是,活动实例将是无用的,因为它的基础上下文不会被设置。
规则是你永远不应该自己创建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方法来获取价值。
将全局文件变为工具类而不是活动,因此可以非常容易地被应用程序的其他部分使用(这不是活动,但可以访问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());
谢谢,但现在它在简单的应用程序中运行良好,但在我使用它的应用程序中抛出错误:java.lang.InstantiationException:无法实例化类com.example.sony.iwashapp.globalfile;没有空的构造函数 进程:com.example.sony.iwashapp,PID:18941 java.lang.RuntimeException:无法实例化应用程序com.example.sony.iwashapp.globalfile:java.lang.InstantiationException:无法实例化类com.example.sony.iwashapp.globalfile;没有空的构造函数 –
嘿谢谢,它现在工作,非常感谢。 –
@PrashantSinghVerma你能接受这个答案吗? – petey
你在哪里调用'setusername()'和'setuserpwd()'? – Lal
如果'globalfile'是实用类,那么为什么要扩展Activity? –