gpt4 book ai didi

java - 为什么我的电子邮件 EditText 保存在我的昵称 EditText 上?

转载 作者:行者123 更新时间:2023-11-30 11:39:10 32 4
gpt4 key购买 nike

我正在努力完成 “Sam 的 24 小时 Android 应用程序开发技术”,但在某些时候我的昵称和电子邮件设置停止正确保存。它现在根本不保存昵称并将电子邮件保存到两者。我做了什么导致这个,我该如何解决?

我认为这些是代码的相关领域,但如果您需要更多请询问。据我所知,这仍然是书中的代码,并且曾经有效。也许某处打字错误?

public class QuizActivity extends Activity {
public static final String GAME_PREFERENCES = "GamePrefs";
public static final String GAME_PREFERENCES_NICKNAME = null; //String
public static final String GAME_PREFERENCES_EMAIL = null; //String

@Override
protected void onPause() {
super.onPause();

EditText nicknameText = (EditText) findViewById(R.id.EditText_Nickname);
EditText emailText = (EditText) findViewById(R.id.EditText_Email);

String strNickname = nicknameText.getText().toString();
String strEmail = emailText.getText().toString();

// TODO: fix password and email saving and displaying improperly
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_NICKNAME, strNickname);
editor.putString(GAME_PREFERENCES_EMAIL, strEmail);
editor.commit();

Toast.makeText(QuizSettingsActivity.this, R.string.settings_saved,
Toast.LENGTH_SHORT).show();

}

public void initNicknameEntry() {
EditText nicknameText = (EditText) findViewById(R.id.EditText_Nickname);
if (mGameSettings.contains(GAME_PREFERENCES_NICKNAME)) {
nicknameText.setText(mGameSettings.getString(
GAME_PREFERENCES_NICKNAME, ""));
}
}

public void initEmailEntry() {
EditText emailText = (EditText) findViewById(R.id.EditText_Email);
if (mGameSettings.contains(GAME_PREFERENCES_EMAIL)) {
emailText.setText(mGameSettings.getString(GAME_PREFERENCES_EMAIL,
""));
}
}

最佳答案

您必须将它们初始化为不同的值:

public static final String GAME_PREFERENCES_NICKNAME = "Nickname"; //String
public static final String GAME_PREFERENCES_EMAIL = "Email"; //String

否则 SharedPreferences 无法区分这两个键,将始终返回找到的第一个键。

关于java - 为什么我的电子邮件 EditText 保存在我的昵称 EditText 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403092/

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