gpt4 book ai didi

java - 条件运行时程序停止

转载 作者:行者123 更新时间:2023-12-02 09:23:33 26 4
gpt4 key购买 nike

   if (TextUtils.isEmpty(value)){
Toast.makeText(this, "Silahkan Isi Jumlah", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

具体代码

btnharga.setOnClickListener {
if (spinner.selectedItem.toString().equals("Ukuran")){
Toast.makeText(this, "Silahkan Isi Ukuran", Toast.LENGTH_SHORT).show()
}
else if (spinner.selectedItem.toString().equals("44")){
if (spinner2.selectedItem.toString().equals("Jenis Laminasi")){
Toast.makeText(this, "Silahkan Isi Laminasi", Toast.LENGTH_SHORT).show()
}
else if (spinner2.selectedItem.toString().equals("Glossy")){
val total : Int
val totall : String
val value = editText.getText().toString()
val finalValue = Integer.parseInt(value)
if (TextUtils.isEmpty(value)){
Toast.makeText(this, "Silahkan Isi Jumlah", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
else if (finalValue < 5){
total = finalValue * 3500
totall = total.toString()
editText2.setText(totall)
spinner.isEnabled = false
spinner2.isEnabled = false
editText.isEnabled = false
editText2.isEnabled = false
}

为什么当这个程序运行时我的应用程序强制关闭?

错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.inisiator.com, PID: 4717
java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:620)
at java.lang.Integer.parseInt(Integer.java:643)
at app.inisiator.com.RequestPinActivity$onCreate$1.onClick(RequestPinActivity.kt:83)
at android.view.View.performClick(View.java:6310)
at android.view.View$PerformClick.run(View.java:24970)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6662)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

最佳答案

发生此问题的原因是您尝试解析无法解析为整数的字符串(空字符串)。

Integer.parseInt 命令移动到下面检查文本是否为空

并记住在从文本中解析数字时捕获 NumberFormatException

if (TextUtils.isEmpty(value)){
Toast.makeText(this, "Silahkan Isi Jumlah", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

try {
val finalValue = Integer.parseInt(value)
if (finalValue < 5){
total = finalValue * 3500
totall = total.toString()
editText2.setText(totall)
spinner.isEnabled = false
spinner2.isEnabled = false
editText.isEnabled = false
editText2.isEnabled = false
}
} catch (ex: NumberFormatException) {
//Add log for parsing error of do something else
}

关于java - 条件运行时程序停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com