Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 整数共享首选项始终返回0_Android_Passwords_Sharedpreferences - Fatal编程技术网

Android 整数共享首选项始终返回0

Android 整数共享首选项始终返回0,android,passwords,sharedpreferences,Android,Passwords,Sharedpreferences,我正在尝试编写一个简单的密码检查代码,以保护我的应用程序的设置。密码应保存在共享首选项中。当SharedReference返回零时,main调用setPassword。这是为了确保当有人第一次尝试输入设置时,会设置密码。然后,setPassword应该设置一个密码,并将其保存在SharedReference中。但在检查是否设置了密码时,SharedReference始终返回0 这是我的主要观点: final Intent intentSetPasswords = new Intent(this,

我正在尝试编写一个简单的密码检查代码,以保护我的应用程序的设置。密码应保存在共享首选项中。当SharedReference返回零时,main调用setPassword。这是为了确保当有人第一次尝试输入设置时,会设置密码。然后,setPassword应该设置一个密码,并将其保存在SharedReference中。但在检查是否设置了密码时,SharedReference始终返回0

这是我的主要观点:

final Intent intentSetPasswords = new Intent(this, SetPasswordActivity.class);
    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    passwordToCheck = sharedPreferences.getInt("PASSWORDSETTINGS", 0);

    //setting button on click listener
    buttonSetings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //check if a password was set muss Überarbeitet werden
            passwordToCheck = sharedPreferences.getInt("PASSWORDSETTINGS", 0);
            Log.e("password", ""+ passwordToCheck);
            if(passwordToCheck==0){
                startActivity(intentSetPasswords);
            }else{
                startActivity(new Intent(MainActivity.this, PasswordCheckActivity.class));
            }
        }
    });
这是我的设置密码:

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

    final Intent intentSettings = new Intent(this, SettingsActivity.class);


    buttonSetPassword = (Button)findViewById(R.id.buttonSetPassword);
    textViewSetPassword = (TextView)findViewById(R.id.textViewSetPasswordText);
    editTextPassword = (EditText)findViewById(R.id.passwordNumericSettingsSetPassword);
    final Intent intent = new Intent(this, MainActivity.class);
    buttonSetPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            counterButton++;
            switch (counterButton){
                case 1:
                    password = editTextPassword.getText().toString();
                    if (password.length() == 4){
                        Log.d("First Password", "taken");
                        buttonSetPassword.setText("Confirm Password!");
                        editTextPassword.setText("");
                    }else{
                        Log.d("First Password", "not taken");
                        textViewSetPassword.setText("The Password must be\nfour numbers long!");
                        password = "";
                        editTextPassword.setText("");
                        counterButton=0;
                    }
                    break;
                case 2:
                    passwordToConfirm = editTextPassword.getText().toString();
                    if (Integer.parseInt(passwordToConfirm)  == Integer.parseInt(password)){

                        Log.e("password", password);
                        Log.e("passwordToConfirm", passwordToConfirm);
                        SharedPreferences sharedPreferences = getSharedPreferences("PASSWORDS", 0);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putInt("PASSWORDSETTINGS", Integer.parseInt(password));
                        editor.commit();
                        startActivity(intentSettings);
                        finish();
                    }else{
                        textViewSetPassword.setText("The passwords aren't the same!\nTry again!");
                        buttonSetPassword.setText("Set Password");
                        editTextPassword.setText("");
                        passwordToConfirm = "";
                        password = "";
                        counterButton = 0;
                        startActivity(intent);
                    }
                    break;
            }
        }
    });
}

我想SharedReference可能有问题,但我不知道该怎么办。

首先,您会得到这样的SharedReference

final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
稍后在设置密码时,您将获得如下密码

SharedPreferences sharedPreferences = getSharedPreferences("PASSWORDS", 0);
它们是两个不同的共享参考。使用第一个SharedReferences。
有关更多信息,请参阅本页

轻松使用下面的库管理数据

首先,检查计数器按钮值,确保执行案例2分支。 其次,用于设置的SharedReferences和用于获取的SharedReferences应该相同。你可以这样做:

private final static String MyPREFERENCES = "MyPrefs"; //define two class fields
private SharedPreferences mSharedPreferences;

mSharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);//in activity Oncreate() methods. 


//and set or get in other place as you want.

你想要什么还不清楚。若共享首选项值未在之前设置,它将返回默认值。在您的情况下,默认值为零。SharedReferences.getIntPASSWORDSETTINGS,getInt的0秒参数是默认值。好的,为什么即使设置了密码,它仍然返回0?在setPassword中,是写入共享首选项的值。但是设置密码后,SharedReference仍然返回零。