无法找出密码检查器API中的问题

时间:2018-07-31 15:21:40

标签: android android-studio

 public class HomePage extends AppCompatActivity{

    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_home_page);
         rbtn=this.findViewById(R.id.rbtn);
         v=this.getCurrentFocus();



    rbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            callLoginDialog();
        }
    });

}
private void callLoginDialog()
{
    myDialog = new Dialog(this);
    myDialog.setContentView(R.layout.register);
    myDialog.setCancelable(true);
    regbtn = (Button) myDialog.findViewById(R.id.rsubmit);

    rmail = (EditText) myDialog.findViewById(R.id.remail);
    rpass = (EditText) myDialog.findViewById(R.id.rpass);
    rcpass=myDialog.findViewById(R.id.rcpass);
    rname=myDialog.findViewById(R.id.rname);
    rphone=myDialog.findViewById(R.id.rphone);
    myDialog.show();
    pB = (ProgressBar) findViewById(R.id.progressBar);
    sv = (TextView) findViewById(R.id.password_strength);

    pchecker=new PasswordChecker(this,v,pB,sv);
    rpass.addTextChangedListener(pchecker);
  }

PasswordChecker

 public class PasswordChecker implements TextWatcher {

    Activity a;
    View v;
    String txt;
    ProgressBar progressBar;
    TextView strengthView;
    public PasswordChecker(Activity a,View v,ProgressBar p,TextView tv) {
        progressBar=p;
        strengthView=tv;
        this.a = a;
        this.v=v;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        Toast.makeText(a, ""+s, Toast.LENGTH_SHORT).show();
        updatePasswordStrengthView(s.toString());//If i comment this line,the app doesn't crash
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }


    private void updatePasswordStrengthView(String password) {


        if (TextView.VISIBLE != strengthView.getVisibility())
            return;

        if (password.isEmpty()) {
            strengthView.setText("");
            progressBar.setProgress(0);
            return;
        }
        PasswordStrength str = PasswordStrength.calculateStrength(password);
        txt=str.getText(a).toString();
        strengthView.setText(txt);
        strengthView.setTextColor(str.getColor());

        progressBar.getProgressDrawable().setColorFilter(str.getColor(), android.graphics.PorterDuff.Mode.SRC_IN);
        if (txt.equals("Weak")) {
            progressBar.setProgress(25);
        } else if (txt.equals("Medium")) {
            progressBar.setProgress(50);
        } else if (txt.equals("Strong")) {
            progressBar.setProgress(75);
        } else {
            progressBar.setProgress(100);
        }
    }
}

我使用了以下API,https://github.com/yesterselga/password-strength-checker-android

此API用于检查密码强度。当我尝试键入密码时,应用程序崩溃。每当我在updatePasswordStrengthView类中评论PasswordChecker方法时,都不会发生此错误。知道updatePasswordStrengthView方法中存在一些错误。请帮帮我。

以下是错误的日志记录:

07-31 21:06:23.280 19825-19825/akshay.shoppingapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: akshay.shoppingapplication, PID: 19825
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at akshay.shoppingapplication.PasswordChecker.updatePasswordStrengthView(PasswordChecker.java:59)
        at akshay.shoppingapplication.PasswordChecker.onTextChanged(PasswordChecker.java:37)

1 个答案:

答案 0 :(得分:3)

使用此

txt=str.getText(a).toString();

代替

txt=str.getText(this).toString();

因为this表示活动(使用应用程序从Intent(或作为清单中的启动器意图)启动时从活动(从应用程序)获取的上下文,但是在您的情况下,它只是充当普通的类,因此将不存在任何上下文,因此this在您的PasswordChecker类中没有上下文

注意:PassWorkChecker仅应包含检查逻辑,因此不应扩展Activity,而应使用a代表上下文的变量,无论何时需要Context