1
我有一个应用程序,我试图在用户登录时保存用户名和密码。当用户启动应用程序时,它应该读取用户名和密码并登录。问题是,它总是读取密码两次 - 一次是密码(这是正确的),一次是用户名。所以它会尝试使用密码作为用户名和密码登录。SharedPreferences读取相同的值
阅读:
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]
初始化你保证'de_letorat_TicTacToe_PREF_NAME'和'de_letorat_TicTacToe_PREF_PASSWORD'有不同的价值观。尝试记录这些 –