gpt4 book ai didi

java - 忽略sharedPreference代码

转载 作者:行者123 更新时间:2023-12-02 11:45:22 25 4
gpt4 key购买 nike

我厌倦了使用sharedpreference来存储 session 并使用Log来测试信息是否被存储。然而,android studio 似乎跳过了涉及共享首选项的过去代码。我做错了什么吗?

全局

public static final String MyPREFERENCES = "MyPrefs" ;
public static final String EmployeeID = "employeeID";
public static final String EmployeeName = "employeeName";
SharedPreferences sharedpreferences;

创建时

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String user = sharedpreferences.getString("EmployeeName", "");
if (user.equalsIgnoreCase("")) {
Log.i("Status", "No session detected");
} else {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}


protected void onPostExecute(Employee results){
if (results != null){

SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString(EmployeeID, results.getEmployeeID());
editor.putString(EmployeeName, results.getFirstName()+" "+results.getLastName());
editor.apply();
editor.commit();
Log.i("aaaaaaaa", "hellloooo"); // It print this libe
String a = sharedpreferences.getString("EmployeeID", "");
Log.i("aaaaaaaa", a); // but not this line
Toast.makeText(LoginActivity.this, " Authenticated", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);

}
else {
Toast.makeText(LoginActivity.this, "Invalid ID", Toast.LENGTH_SHORT).show();
}
}

最佳答案

尝试使用 PreferenceManager.getDefaultSharedPreferences(context) 并查看是否可以解决您的问题。基本上是一样的,唯一的区别是用这种方式你在默认的首选项文件中写入。

关于java - 忽略sharedPreference代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com