共享问题

时间:2015-04-18 19:52:34

标签: java android sharedpreferences

我正在创建一个应用程序并使用共享首选项。前两个活动是登录屏幕和重置密码屏幕。 登录用户名和密码最初是"用户"。当用户登录时,他们被重定向到重置密码活动,他们必须通过输入密码更改密码,确认密码,并输入原始密码(用户)。 新密码将保存到共享首选项,从而覆盖原始密码。

我的问题是,当我在模拟器上运行应用程序时,无论我使用什么用户名和密码,它每次都会崩溃。到目前为止,这是代码:

登录活动

 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();  
            }           

        }   

    }
}

任何帮助都会非常感谢大家。

1 个答案:

答案 0 :(得分:0)

通过查看您的代码应用程序崩溃在这些行bcz您正在创建/初始化local variable具有相同的实例变量名称即。 appPrefs和实例变量appPrefs仍为空(默认值)

1。 登录活动

String existingPassword = appPrefs.getString(SAVED_PASSWORD,null); 

2。 ResetPassword 活动

 String existingPassword = appPrefs.getString(SAVED_PASSWORD,null);

原因:

对于这两个活动,您没有初始化appPrefs对象,这是一个实例变量

解决方案:

    protected void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_reset_password);

            // get shared prefs for this application
            //your code creates a new local variable with same instance variable name ie. appPrefs
            //SharedPreferences appPrefs = getSharedPreferences(SAVED_PASSWORD, 0);

           //changes  : init instance variable 
           appPrefs = getSharedPreferences(SAVED_PASSWORD, 0);
        }