Android - 拒绝空输入问题
我目前正在尝试让我的代码拒绝空(空)输入,每次我输入任何值时它都会崩溃,但是告诉它拒绝“0”有效(如代码的 else if 部分所示)。
boolean check_input1 = Add.this.input.getText().toString().equals(null);
boolean check_input2 = Add.this.input2.getText().toString().equals(null);
boolean check_input3 = Add.this.input3.getText().toString().equals(null);
float pricecheck_input2 = Float.valueOf(Add.this.input2.getText().toString().trim()).floatValue();
float pricecheck_input3 = Float.valueOf(Add.this.input3.getText().toString().trim()).floatValue();
if (check_input1 == true | check_input2 == true | check_input3 == true) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);
// set the message to display
alertbox.setMessage("No fields may be blank");
alertbox.show();
}
else if (pricecheck_input2 == 0 | pricecheck_input3 == 0) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);
// set the message to display
alertbox.setMessage("Prices must be greater than 0");
alertbox.show();
}
else {
new InsertDataTask().execute(Add.this.input.getText().toString(), Float.toString(abv_ppl_calculation), Add.this.input3.getText().toString());
}
帮助将不胜感激!
I am currently trying to get my code to reject a null (empty) input, every time I enter nothing for values it crashes, however telling it to reject "0" worked (as seen in the else if part of the code).
boolean check_input1 = Add.this.input.getText().toString().equals(null);
boolean check_input2 = Add.this.input2.getText().toString().equals(null);
boolean check_input3 = Add.this.input3.getText().toString().equals(null);
float pricecheck_input2 = Float.valueOf(Add.this.input2.getText().toString().trim()).floatValue();
float pricecheck_input3 = Float.valueOf(Add.this.input3.getText().toString().trim()).floatValue();
if (check_input1 == true | check_input2 == true | check_input3 == true) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);
// set the message to display
alertbox.setMessage("No fields may be blank");
alertbox.show();
}
else if (pricecheck_input2 == 0 | pricecheck_input3 == 0) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);
// set the message to display
alertbox.setMessage("Prices must be greater than 0");
alertbox.show();
}
else {
new InsertDataTask().execute(Add.this.input.getText().toString(), Float.toString(abv_ppl_calculation), Add.this.input3.getText().toString());
}
Help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要检查空检查的逻辑。您有:
问题是
toString()
如何导致null
?您的代码处于正确的轨道上,因为稍加挖掘就会发现.getText()
永远不会返回null
(它始终是CharSequence
)。检查的右侧应该检查空字符串。例如:You need to examine the logic for your null check. You have:
The question is how would a
toString()
to result innull
? Your code is on the right track since a little digging shows that.getText()
will never returnnull
(it's always aCharSequence
). The right side of your check should instead check for the empty string. E.g.: