【问题标题】:Unable to access a string from SharedPreferences in Android无法从 Android 中的 SharedPreferences 访问字符串
【发布时间】:2016-11-19 17:13:29
【问题描述】:

所以我有两个屏幕,一个设置密码屏幕和一个输入密码(登录)屏幕。如果没有密码(即从未使用过该应用程序),我希望它弹出输入密码屏幕,然后在随后启动时进入登录屏幕。如果用户输入密码,它会与 sharepreferences 中存储的值进行比较,如果正确则继续菜单。但是使用设置密码屏幕后,登录屏幕似乎无法访问密码。

如何让登录界面看到注册页面设置的密码?

这是创建密码的代码:

public class CreatePassword extends Activity {
public EditText setPass1, setPass2;
String newPass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_createpassword);

    setPass1 = (EditText)findViewById(R.id.editTextSetPassword);
    setPass2 = (EditText)findViewById(R.id.editTextRepeatSetPassword);
}

public void btnSubmitNewPassword(View v){
    if(setPass1.getText().toString() != "") {
        if (setPass1.getText().toString().equals(setPass2.getText().toString())) {
            newPass = setPass1.getText().toString();

            SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("sharedPref", MODE_PRIVATE);
            SharedPreferences.Editor prefEditor = sharedPref.edit();
            prefEditor.putString("password", newPass);
            prefEditor.apply();
            Toast.makeText(getApplicationContext(), newPass, Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(), "New Password Set", Toast.LENGTH_LONG).show();
            startActivity(new Intent(CreatePassword.this, Menu.class));
        } else {
            Toast.makeText(getApplicationContext(), "Passwords don't match!", Toast.LENGTH_LONG).show();
        }
    }
    else{
        Toast.makeText(getApplicationContext(), "Please Enter Desired Password", Toast.LENGTH_LONG).show();
    }
}
}//end CreatePassword class

以及拒绝使用刚刚设置的密码的EnterPassword(登录)界面:

public class EnterPassword extends Activity {

EditText editTextPasswordAttempt;
SharedPreferences sharedPref;
String passwordAttempt, passwordActual;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enterpassword);

    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    boolean passwordExists = sharedPref.contains("password");

    if(passwordExists == false){
        startActivity(new Intent(EnterPassword.this, CreatePassword.class));
    }

    editTextPasswordAttempt = (EditText)findViewById(R.id.editTextPassword);
}

public void btnSubmitToMenu(View v){

    passwordActual = sharedPref.getString("password", );
    Toast.makeText(getApplicationContext(), passwordActual, Toast.LENGTH_LONG).show();
    passwordAttempt = editTextPasswordAttempt.getText().toString();

    if(passwordAttempt.equals(passwordActual)) {
        startActivity(new Intent(EnterPassword.this, Menu.class));
    }
    else{
        Toast.makeText(getApplicationContext(), "Invalid Password", Toast.LENGTH_LONG).show();
    }
}
}

【问题讨论】:

    标签: android android-activity login sharedpreferences


    【解决方案1】:

    在访问 SharedPreferences 值时,您需要使用相同的 name SharedPreferences。在 EnterPassword 活动中使用相同的方式访问 SharedPreferences,使用以下行:

    getApplicationContext().getSharedPreferences("sharedPref", MODE_PRIVATE);
    

    【讨论】:

      【解决方案2】:

      您正在使用 2 个不同的 SharedPreferences。

      CreatePassword你做的活动:

      getApplicationContext().getSharedPreferences("sharedPref", MODE_PRIVATE);
      

      但是在 EnterPassword 活动中你做的:

      PreferenceManager.getDefaultSharedPreferences(this);
      

      如果您在两个活动中使用同一行,它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多