public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
public EditText studentNumber, passWord;
//defining AwesomeValidation object
public AwesomeValidation awesomeValidation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
awesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
studentNumber = (EditText) findViewById(R.id.studentNumber);
passWord = (EditText) findViewById(R.id.passWord);
awesomeValidation.addValidation(this, R.id.studentNumber, "^[0-9]{9}", R.string.studentnumberError);
String regexPassword = ".{8,}";
awesomeValidation.addValidation(this, R.id.passWord, regexPassword, R.string.invalid_password);
signIn.setOnClickListener(this);
}
// Triggers when LOGIN Button clicked
@Override
public void onClick(View view) {
if (view == signIn) {
checkLogin();
}
}
public void checkLogin() {
// Get text from studentNumber and passWord field
final String studentNumber = studentNumber.getText().toString();
final String passWord = passWord.getText().toString();
// Initialize AsyncLogin() class with studentNumber and password
new AsyncLogin().execute(studentNumber,passWord);
}
}
Android Studio表示可能尚未初始化变量studentNumber和passWord。错误显示在这一行:
final String studentNumber = studentNumber.getText().toString();
final String passWord = passWord.getText().toString();
似乎有什么问题?它是表格的验证吗?谢谢。
答案 0 :(得分:2)
在onClick方法中更改字符串变量名称,它与edittext变量名称冲突:
public void checkLogin() {
// Get text from studentNumber and passWord field
final String studentNumberString = studentNumber.getText().toString();
final String passWordString = passWord.getText().toString();
// Initialize AsyncLogin() class with studentNumber and password
new AsyncLogin().execute(studentNumberString,passWordString);
}
}
答案 1 :(得分:1)
你得到变量“studentNumber”和“passWord”可能尚未初始化?
因为您的String变量名和您的Edit-text名都相同所以它们是冲突的
只需更改您的String变量名称
使用此
final String studentNumber2 = studentNumber.getText().toString();
final String passWord2 = passWord.getText().toString();
而不是
final String studentNumber = studentNumber.getText().toString();
final String passWord = passWord.getText().toString();
答案 2 :(得分:0)
您收到错误,因为字符串名称和编辑文字名称是示例请更改您的方法。
public void checkLogin() {
// Get text from studentNumber and passWord field
final String studentNum = studentNumber.getText().toString();
final String pass= passWord.getText().toString();
// Initialize AsyncLogin() class with studentNumber and password
new AsyncLogin().execute(studentNum,pass);
}