I'm working on an android studio project using sharedpreferences that is a login/register simple application.
I have an activity for changing the password for an existed user and it keeps getting errors and crashes.
Main activity:
public class MainActivity extends AppCompatActivity {
TextView txtv;
Button chgpwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtv = (TextView) findViewById(R.id.txtvSignUp);
txtv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
NewUserActivity.class);
startActivity(i);
finish();
}
});
chgpwd = (Button) findViewById(R.id.changePwd);
chgpwd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent c = new Intent(getApplicationContext(),
PasswordUpdateActivity.class);
startActivity(c);
finish();
}
});
}
public void usrLogIn(View v) {
// To link to the the view objects and read data
EditText uname = (EditText) findViewById(R.id.nameInp);
EditText upass = (EditText) findViewById(R.id.pwdInp);
String username = uname.getText().toString();
String userpass = upass.getText().toString();
// To open sharedPref, Create if not exist
SharedPreferences userAcc = getSharedPreferences("UsersAccounts", 0);
// To Check if user name exists in the sharedPref.
if (!userAcc.contains(username)) {
Toast.makeText(getApplicationContext(), "User NOT Exists",
Toast.LENGTH_LONG).show();
return;
}
// To chaeck if the entered password match the password stored in
// SharedPref object
if (userAcc.getString(username, null).equals(userpass)) {
Toast.makeText(getApplicationContext(), "Access Authenticated",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Password is wrong",
Toast.LENGTH_LONG).show();
}
}
}
New user activity:
public class NewUserActivity extends AppCompatActivity {
SharedPreferences usrAccs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_user);
}
public void signUp(View v) {
// Read username and password from input fields
EditText usr = (EditText) findViewById(R.id.usrInp);
EditText pwd = (EditText) findViewById(R.id.pwdInp);
// Define SharedPreference object
usrAccs = getApplicationContext().getSharedPreferences("UsersAccounts", 0);
// To create editor object for sharedPref
SharedPreferences.Editor userEdit = usrAccs.edit();
//To Check if user name already exists
if (usrAccs.contains(usr.getText().toString())) {
Toast.makeText(getApplicationContext(), "User already Exists",
Toast.LENGTH_LONG).show();
return;
}
// If It pass the if statement, it means the user does not exist
// and store info in sharedPref using Editor
userEdit.putString(usr.getText().toString(),pwd.getText().toString());
userEdit.commit();
Toast.makeText(getApplicationContext(), usr.getText().toString() + " Account is created", Toast.LENGTH_LONG).show();
// To return To the main activity
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
}
and lastly the password changing activity:
public class PasswordUpdateActivity extends AppCompatActivity {
SharedPreferences usrAccu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password_update);
}
public void usrUpdate(View v) {
// To link to the the view objects and read data
EditText userr = (EditText) findViewById(R.id.etpwd);
EditText pwdrr = (EditText) findViewById(R.id.etname);
Button btnup = (Button) findViewById(R.id.btnChange);
String newpwd = pwdrr.getText().toString();
String updatedPwd = usrAccu.getString("userpass", "");
usrAccu = PasswordUpdateActivity.this.getSharedPreferences("UsersAccounts", 0);
//To Check if user name already exists
if (usrAccu.contains(userr.getText().toString())) {
SharedPreferences.Editor editor = usrAccu.edit();
editor.putString("userpass", newpwd);
editor.commit();
Toast.makeText(getApplicationContext(), "Password Updated", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "User Doesn't Exists", Toast.LENGTH_LONG).show();
}
}
}
I think that you first must create your usrAccu.
public void usrUpdate(View v) {
// To link to the the view objects and read data
EditText userr = (EditText) findViewById(R.id.etpwd);
EditText pwdrr = (EditText) findViewById(R.id.etname);
Button btnup = (Button) findViewById(R.id.btnChange);
usrAccu = PreferenceManager.getDefaultSharedPreferences(this);
String newpwd = pwdrr.getText().toString();
String updatedPwd = usrAccu.getString("userpass", "");
//To Check if user name already exists
....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.