【问题标题】:How do you encompass a try/catch block in these functions?你如何在这些函数中包含一个 try/catch 块?
【发布时间】:2020-09-06 00:48:23
【问题描述】:

如果应用程序中的输入字段留空,我会收到 NumberFormatException。以前,我有一个可以处理它的工作 try/catch 块,但我需要在 if/else 语句上添加一个开关来确定要运行哪个函数。我已经尝试了我能想到的一切来重新格式化 try/catch 以便处理错误,但一切都只是破坏了应用程序。我在这里做错了什么,或者甚至可以做什么?

package com.WordPlay.awcc

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class Setup3 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main
    )

    editTextNumber2.text.toString().toInt()
    editTextNumber3.text.toString().toInt()
    editTextNumber4.text.toString().toInt()
    editTextNumber5.text.toString().toInt()
    editTextNumber6.text.toString().toInt()
    editTextNumber15.text.toString().toInt()
}
}


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    /// Define button for checked
    fun essentialexpert() {
        val int11 = editTextNumber.text.toString().toInt()
        val pay21 = 5
        val product11 = int11 * pay21

        val int12 = editTextNumber2.text.toString().toInt()
        val pay22 = 12.5
        val product12 = int12 * pay22

        val int13 = editTextNumber3.text.toString().toInt()
        val pay23 = 15
        val product13 = int13 * pay23

        val int16 = editTextNumber6.text.toString().toInt()
        val pay24 = 5
        val product14 = int16 * pay24

        val int15 = editTextNumber5.text.toString().toInt()
        val pay25 = 5
        val product15 = int15 * pay25

        val int14 = editTextNumber4.text.toString().toInt()
        val pay26 = 2
        val product16 = int14 / pay26

        val final =
            product11 + product12 + product13 + product14 + product15 + product16
        val complete = final.toString()
        try {
            editTextNumber15?.setText(complete)
        } catch (e: NumberFormatException) {
            Toast.makeText(
                applicationContext,
                "Please enter a 0 in the blank field",
                Toast.LENGTH_LONG
            ).show()
        }
    }

    /// Define function for unchecked
    fun essential() {
        val int1 = editTextNumber.text.toString().toInt()
        val pay1 = 5
        val product1 = int1 * pay1

        val int2 = editTextNumber2.text.toString().toInt()
        val pay2 = 7.5
        val product2 = int2 * pay2

        val int3 = editTextNumber3.text.toString().toInt()
        val pay3 = 10
        val product3 = int3 * pay3

        val int6 = editTextNumber6.text.toString().toInt()
        val pay4 = 5
        val product4 = int6 * pay4

        val int5 = editTextNumber5.text.toString().toInt()
        val pay5 = 0
        val product5 = int5 * pay5

        val int4 = editTextNumber4.text.toString().toInt()
        val pay6 = 2
        val product6 = int4 / pay6

        val final = product1 + product2 + product3 + product4 + product5 + product6
        val complete = final.toString()
        try {
            editTextNumber15?.setText(complete)
        } catch (e: NumberFormatException) {
            Toast.makeText(
                applicationContext,
                "Please enter a 0 in the blank field",
                Toast.LENGTH_LONG
            ).show()
        }
    }

    try {
        essential()
    } catch (e: java.lang.NumberFormatException) {
        Toast.makeText(
            applicationContext,
            "Please enter a 0 in the blank fields",
            Toast.LENGTH_LONG
        ).show()

        /// Define buttons to change activity

        val button2 = findViewById<Button>(R.id.button2)
        button2.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
        val button3 = findViewById<Button>(R.id.button3)
        button3.setOnClickListener {
            val intent2 = Intent(this, ThirdActivity::class.java)
            startActivity(intent2)
        }
        val button = findViewById<Button>(R.id.button)
        button.setOnClickListener {
            if (switch1.isChecked) {
                essentialexpert()
            } else {
                essential()

            }
        }
    }
}

}

【问题讨论】:

    标签: android xml kotlin try-catch


    【解决方案1】:

    您不应该在写入EditText 时捕获错误,而是在读取时捕获错误。类似的东西:

    fun essentialexpert() {
        val int11 = try {
            editTextNumber.text.toString().toInt()
        } catch (e: NumberFormatException) {
            Toast.makeText(
                applicationContext,
                "Please enter a 0 in the blank field",
                Toast.LENGTH_LONG ).show()
    
            return
        }
        val pay21 = 5
        val product11 = int11 * pay21
    
        // ...
    
        // If you reach here, all fields are ok
        val final = product11 + /* ... */ + product16
        val complete = final.toString()
        editTextNumber15?.setText(complete)
    }
    

    借助 Kotlin,您可以使用 toIntOrNull 方法简化此 try/catch。

    var areAllFieldsValid = false
    
    fun essentialexpert() {
        val int11 = editTextNumber.text.toString().toIntOrNull() ?: return
        val pay21 = 5
        val product11 = int11 * pay21
    
        // ...
    
        // If you reach here, all fields are ok
        areAllFieldsValid = true
        val final = product11 + /* ... */ + product16
        val complete = final.toString()
        editTextNumber15?.setText(complete)
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        // ...
        btnFinish.setOnClickListener {
            essentialexpert()
            if (!areAllFieldsValid) {
                Toast.makeText(
                    applicationContext,
                    "Please enter a 0 in the blank fields",
                    Toast.LENGTH_LONG).show()
    
            } else {
                // Do something when all fields are valid
            }
        }
    }
    

    给你的一些提示:

    • final.toString() 替换为"$final"

    • Toast.makeText(applicationContext, ... 替换为Toast.makeText(this, ...ActivityContext

    • 我不确定Setup3 是什么,但如果它只是为了获取EditText 值而存在,它将无法工作。您可以在MainActivity 中进行操作。

    • 表达式5/2 可能不会返回您认为它返回的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      相关资源
      最近更新 更多