gpt4 book ai didi

java - SharedPreferences 读取相同的值

转载 作者:行者123 更新时间:2023-11-30 03:12:29 26 4
gpt4 key购买 nike

我有一个应用程序,我试图在用户登录时保存用户名和密码。当用户启动该应用程序时,它应该读取用户名和密码并登录。问题是,它总是读取密码两次——一次读取密码(正确),一次读取用户名。因此它尝试使用密码作为用户名和密码登录。

阅读:

sharedPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

String userName = sharedPref.getString(getString(
R.string.de_letorat_TicTacToe_PREF_NAME), null);
if(userName != null) {
String password = sharedPref.getString(getString(
R.string.de_letorat_TicTacToe_PREF_PASSWORD), null);
Log.d("TicTacToe", "Read. Username: " + userName + "; Password: " + password);
pw.println("$login" + userName + "/" + password);
pw.flush();
Log.d("TicTacToe", "Sent. Username: " + userName + "; Password: " + password);
}

保存:

Editor edit = sharedPref.edit();

EditText nameField = (EditText) findViewById(R.id.login_fragment_username);
EditText passwordField = (EditText) findViewById(R.id.login_fragment_password);

edit.putString(getString(R.string.de_letorat_TicTacToe_PREF_NAME),
nameField.getText().toString());
edit.putString(getString(R.string.de_letorat_TicTacToe_PREF_PASSWORD),
passwordField.getText().toString());
edit.commit();

Log.d("TicTacToe", "Wrote. Username: " + nameField.getText().toString() +
"; Password: " + passwordField.getText().toString());

结果是:

Wrote. Username: [username]; Password: [password]
Read. Username: [password]; Password: [password]
Sent. Username: [password]; Password: [password]

最佳答案

检查您的 preferences.xml 或 strings.xml 是否有重复键。

您的偏好键似乎都是“密码”。

关于java - SharedPreferences 读取相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708418/

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