I am creating an application and am using shared prefs. The first two activities are a login screen and a reset password screen. The login username and password initially are "user".When the user logs on, they are redirected to the reset password activity where they must change the password by entering a password, confirm it, and also enter the original password (user). The new password will be saved to shared preferences thus overwriting the original password.
My issue is that when i run the app on the emulator it crashes every time irrespective of what username and password i use.. Here is the code so far:
LOGIN ACTIVITY
public class SignIn extends Activity {
static final String MY_PREFS = "Prefs";
static final String SAVED_PASSWORD = "Password";
// Object used for Shared Preferences
SharedPreferences appPrefs;
// First method called when the activity is opened protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_in);
// get Shared Preferences for this application
SharedPreferences appPrefs = getSharedPreferences(SAVED_PASSWORD, 0);
}
// Called when the "LOGIN" button is pressed
public void loginBtnClicked(View v){
// The username and password that is to be used on initial use of the application
String userName = "user";
String original_password = "user";
// Get the settings for the password string from the prefs and assign a name
String existingPassword = appPrefs.getString(SAVED_PASSWORD,null);
// Widgets used during the login phase
EditText usernameField = (EditText)findViewById(R.id.editText_username);
EditText passwordField = (EditText)findViewById(R.id.editText_password);
Button reset =(Button)findViewById(R.id.goToReset);
Button next =(Button)findViewById(R.id.next);
// Get the text from the editText fields,convert to a string, and assign a name
String name = usernameField.getText().toString();
String pass = passwordField.getText().toString();
// If either field is empty inform user and return to give them another go
if (name.equals(null)|| pass.equals(null)){
Toast.makeText(this,"ERROR!!, all fields must be filled",Toast.LENGTH_SHORT).show();
return;
}
//========================================
// VALIDATION OF LOGIN INFORMATION SUBMITTED
//========================================
// If there NOT information saved to preferences.
if (existingPassword.equals(null)){
// IF the username and password are "USER"
if(name.equals(userName) && pass.equals(original_password)){
// Create an intent to open the next window
Intent reset_intent = new Intent(this,ResetPassword.class);
// Start the new activity
startActivity(reset_intent);
}
}
//If there IS information saved to preferences.
else if (existingPassword!=null){
//IF the username is "USER"...
if(name.equals(userName) ){
// If the password is what is saved to preferences.
if(pass.equals(existingPassword)){
// Make buttons visible for user choice
reset.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
}
else{
// Display a toast pop-up to show the text from the intent
Toast.makeText(this,"ERROR!!,password incorrect",Toast.LENGTH_SHORT).show();
}
}
else{
// Display a toast pop-up to show the text from the intent
Toast.makeText(this,"ERROR!!,username incorrect",Toast.LENGTH_SHORT).show();
}
}
}
// Called when the "GO TO RESET" button is pressed
public void ResetBtnClicked(View v){
// Create an intent to open the next window
Intent reset_intent = new Intent(this,ResetPassword.class);
// Start the new activity
startActivity(reset_intent);
}
//Called when the "NEXT" button is pressed
public void onNextBtnClicked (){
// Create an intent to open the next window
Intent personal_intent = new Intent(this,PersonalDetails.class);
// Start the new activity
startActivity(personal_intent);
}
}
public class ResetPassword extends Activity {
public static final String MY_PREFS = "Prefs";
public static final String SAVED_PASSWORD = "Password";
// for shared preferences
SharedPreferences appPrefs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reset_password);
// get shared prefs for this application
SharedPreferences appPrefs = getSharedPreferences(SAVED_PASSWORD, 0);
}
//Save the new password
public void resetBtnClicked(View v){
// Widgets used during password reset.
EditText newPassword = (EditText)findViewById(R.id.editText_newPassword);
EditText confirmPassword = (EditText)findViewById(R.id.editText_retypePassword);
EditText currentPassword = (EditText)findViewById(R.id.editText_existingPassword);
// Get the text from the editText fields,convert to a string, and assign a name
String newPass = newPassword.getText().toString();
String confirmPass = confirmPassword.getText().toString();
String currentPass = currentPassword.getText().toString();
// get the settings for the password string from the prefs
String existingPassword = appPrefs.getString(SAVED_PASSWORD,null);
//========================================
// VALIDATION OF PASSWORD INFORMATION SUBMITTED
//========================================
// if the current password submitted is the same as the saved password
if(currentPass.equals(existingPassword)){
//if the new password is the same in both the new and re-enter fields
if(newPass.equals(confirmPass)){
//save the preferences
//get the editor for prefs
Editor prefsEditor = appPrefs.edit();
// update the prefs with the new settings
prefsEditor.putString(SAVED_PASSWORD,newPass);
prefsEditor.commit();
// Create an intent to open the next window
Intent personal_intent = new Intent(this,PersonalDetails.class);
// display a toast pop-up to show the text from the intent
Toast.makeText(this,"New password saved",Toast.LENGTH_SHORT).show();
// Start the new activity
startActivity(personal_intent);
}
// If the passwords do not match
else {
// display a toast pop-up to show the text from the intent
Toast.makeText(this,"Passwords do not match!!",Toast.LENGTH_SHORT).show();
}
}
// if the current password is incorrect
else{
// display a toast pop-up to show the text from the intent
Toast.makeText(this,"Incorrect password used!!",Toast.LENGTH_SHORT).show();
}
// If there is no password saved to prefs.
if (existingPassword.equals(null)){
if(newPass.equals(confirmPass)){
//save the preferences
//get the editor for prefs
Editor prefsEditor = appPrefs.edit();
// update the prefs with the new settings
prefsEditor.putString(SAVED_PASSWORD,newPass);
prefsEditor.commit();
// Create an intent to open the next window
Intent personal_intent = new Intent(this,PersonalDetails.class);
// display a toast pop-up to show the text from the intent
Toast.makeText(this,"New password saved",Toast.LENGTH_SHORT).show();
// Start the new activity
startActivity(personal_intent);
}
else{
// display a toast pop-up to show the text from the intent
Toast.makeText(this,"Passwords do not match!!",Toast.LENGTH_SHORT).show();
}
}
}
}
Any help would be greatly appreciated guys.
0 comments:
Post a Comment