You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中TextInputLayout密码与确认密码验证功能异常求助

Fixing Password & Confirm Password Validation with TextInputLayout

Hey there! Let's get your password confirmation validation working properly—your current crash is caused by a simple oversight, so we'll fix it step by step.

What's Causing the Crash?

In your Verify() method, you're using variables CfmPassword and Password that never got initialized! These aren't linked to your actual TextInputLayout widgets, so when you call .getText() on them, you hit a NullPointerException and the app crashes. Also, you used the bitwise OR operator (|) instead of the logical OR operator (||) in your Reg() method—|| is what you want for conditional checks like this.

Fixed Code Solution

First, let's rewrite the validation methods to use the TextInputLayout objects you already initialized. Then we'll clean up the validation flow:

public class Register extends AppCompatActivity {
    private TextInputLayout textInputRegPassword;
    private TextInputLayout textInputRegCfmPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_register);
        
        // Reuse these initialized references for all validation checks
        textInputRegPassword = findViewById(R.id.text_input_RegPassword);
        textInputRegCfmPassword = findViewById(R.id.text_input_RegCfmPassword);
    }

    // Check if password field is not empty
    private boolean isPasswordValid(){
        String password = textInputRegPassword.getEditText().getText().toString().trim();
        if(password.isEmpty()){
            textInputRegPassword.setError("Enter Password");
            return false;
        } else {
            textInputRegPassword.setError(null);
            return true;
        }
    }

    // Check if confirm password field is not empty
    private boolean isConfirmPasswordValid(){
        String confirmPassword = textInputRegCfmPassword.getEditText().getText().toString().trim();
        if(confirmPassword.isEmpty()){
            textInputRegCfmPassword.setError("Enter Confirm Password");
            return false;
        } else {
            textInputRegCfmPassword.setError(null);
            return true;
        }
    }

    // Check if password and confirm password match
    private boolean doPasswordsMatch(){
        String password = textInputRegPassword.getEditText().getText().toString().trim();
        String confirmPassword = textInputRegCfmPassword.getEditText().getText().toString().trim();
        
        if(!password.equals(confirmPassword)){
            textInputRegCfmPassword.setError("Passwords Do Not Match");
            return false;
        } else {
            textInputRegCfmPassword.setError(null);
            return true;
        }
    }

    public void Reg(View v){
        // Use logical OR (||) instead of bitwise OR (|) for proper conditional checks
        if(!isPasswordValid() || !isConfirmPasswordValid()){
            return;
        }
        
        // Only check password match if both fields are already non-empty
        if(!doPasswordsMatch()){
            return;
        }
        
        // If all validations pass, add your registration logic here
        // Example: start a new activity, call an API, etc.
    }
}

Key Improvements:

  • Renamed methods to be more descriptive (e.g., isPasswordValid() instead of RegisterPassword())
  • Fixed the password match check to use your existing, properly initialized TextInputLayout references
  • Swapped | to || for correct conditional short-circuiting
  • Added a clear validation flow: first ensure fields aren't empty, then check for password consistency
  • Ensured error messages are set and cleared appropriately for each validation step

XML Note

Your activity_register.xml is functional as-is, but if you're working with a newer Android project, consider migrating to AndroidX components (replace android.support.design.widget.TextInputLayout with com.google.android.material.textfield.TextInputLayout and similarly for TextInputEditText).

内容的提问来源于stack exchange,提问作者LEGEND PREDATOR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:59:44