空的android edittext不断崩溃的应用程序,尽管处理了错误



单击按钮时,我正在从两个edittext字段中收集值。我已经实现了对所有必要条件(负数、值太大、零(的处理,但具有空edittext的情况不断崩溃,尽管也处理了这种情况。

fun buildAlertDialog(message : String = "Bad input value")
{
val alertDialogBuilder = AlertDialog.Builder(this@MainActivity)
alertDialogBuilder.setTitle("Error")
alertDialogBuilder.setMessage("$message")
alertDialogBuilder.setNeutralButton("Close"){_,_ ->}
val alertDialog : AlertDialog = alertDialogBuilder.create()
alertDialog.show()
}
btnCalculate.setOnClickListener{
var heightInput = editTxt_EnterHeight.text.toString().toDouble()
var weightInput = editTxt_EnterWeight.text.toString().toInt()
if(editTxt_EnterHeight.toString().trim().isNotEmpty())
{
Toast.makeText(this, "Height is not empty", Toast.LENGTH_SHORT).show()
if(heightInput > 2.5)
{
buildAlertDialog("Bad input value - too tall")
}
else if(heightInput == 0.0)
{
buildAlertDialog("Bad input value - height value can't be zero")
}
else if(heightInput < 0)
{
buildAlertDialog("Bad input value - height can't be a negative number")
}
}
else if(editTxt_EnterHeight.text.toString().trim().length == 0)
{
buildAlertDialog("Plese enter height value")
}
if(editTxt_EnterWeight.toString().trim().isNotEmpty())
{
Toast.makeText(this, "Weight is not empty", Toast.LENGTH_SHORT).show()
if(weightInput > 350)
{
buildAlertDialog("Bad input value - too big weight value")
}
else if(weightInput == 0)
{
buildAlertDialog("Bad input value - weight can't be zero")
}
else if(weightInput < 0)
{
buildAlertDialog("Bad input value - weight can't be a negative number")
}
}
else if(editTxt_EnterWeight.text.toString().trim().length == 0)
{
buildAlertDialog("Please enter weight value")
}
}

高度编辑文本inputType设置为numberDecimal,而重量编辑文本设置为number。

我尝试过在stackoverflow和其他网站上实现多个解决方案,但所有的解决方案都有相同的结果。(检查EditText是否为空kotlin android,https://www.tutorialspoint.com/how-to-check-if-android-edittext-is-empty,https://inducesmile.com/kotlin-source-code/how-to-check-if-edittext-is-empty-in-kotlin/)

Bro在将文本强制转换为双精度之前必须检查文本,而不是之后。向下移动:

var heightInput = editTxt_EnterHeight.text.toString().toDouble()
var weightInput = editTxt_EnterWeight.text.toString().toInt()

最新更新