java如何在另一个活动中检索SharedReference文件的值?
Activity 1st..
Here this is my first activity to add data
preferences=PreferenceManager.getDefaultSharedPreferences(context);
preferences = getPreferences(MODE_PRIVATE);
editor = preferences.edit();
editor.putString("userid",et_username.getText().toString());//adduserid
editor.putString("password",et_password.getText().toString());//add password
editor.commit();
活动2 这是我检索数据的第二个活动
String userName=preferences.getString("userid","");
String password=preferences.getString("password","");
Log.d("user : second", ""+preferences.getString("userid",""));
Log.d("password : second", ""+preferences.getString("password",""));
由于空值,此处不显示日志
# 1 楼答案
我只存储一个整数值,您必须在其中存储多个值
在要在其中使用的
preferenceConnector
类下面然后,你还可以将股票优先权价值用于其他活动,如以下
希望以上代码有用
# 2 楼答案
你能试试这条路吗
# 3 楼答案
检查
preferences
对象(可能是null
)。这可能就是问题所在,因为其他字符串变量从不为null,所以可以是空字符串(""
)在本例中,您是否缺少第二个
Activity
中preferences
的初始化# 4 楼答案
在这两个活动中,只需使用此项即可获取SharedReferences对象:
SharedPreferences prefs = getSharedPreferences("PREFS", Context.MODE_PRIVATE);
可能是您试图从不同的活动访问不同的首选项文件
或者只是使用