当我按下按钮对两个值求和时应用程序崩溃
当我尝试按下按钮对编辑文本求和时,我的应用程序崩溃了,但我不知道如何修复,因为我看不到应用程序日志。 我如何检查我的日志?
我的代码(MainActivity.kt
):
package com.example.paymentothetrip
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.activity_main)
var kmtotal = findViewById<EditText>(R.id.ikmtotal)
var ilitro = findViewById<EditText>(R.id.ilitro)
var ikmforlit = findViewById<EditText>(R.id.ikmforlit)
val btncalcular: TextView = findViewById<Button>(R.id.btncalcular)
btncalcular.setOnClickListener{
calcular(kmtotal, ikmforlit, ilitro, )
}
}
@SuppressLint("SetTextI18n")
private fun calcular(kmtotal: EditText, ikmforlit: EditText, ilitro: EditText) {
val txtresult = findViewById<TextView>(R.id.txtresult)
var result: Double = (kmtotal.text.toString().toDouble() / ikmforlit.text.toString().toDouble()) * ilitro.toString().toDouble()
txtresult.text = "R$ $result"
}
}
LOGCAT:
2022-02-26 21:16:10.655 19167-19167/com.example.paymentothetrip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paymentothetrip, PID: 19167
java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{28c5fa0 VFED..CL. ........ 0,745-1080,883 #7f0a01df app:id/ilitro aid=1073741825}"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.paymentothetrip.MainActivity.calcular(MainActivity.kt:32)
at com.example.paymentothetrip.MainActivity.onCreate$lambda-0(MainActivity.kt:24)
at com.example.paymentothetrip.MainActivity.$r8$lambda$x4PiI7hxLHE9UUw2OiqZu2i-kE4(Unknown Source:0)
at com.example.paymentothetrip.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:8)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2022-02-26 21:16:10.670 19167-19167/com.example.paymentothetrip I/Process: Sending signal. PID: 19167 SIG: 9
My app crash when I try to press the button to sum the edittext and I don't know how to fix because I can't see the app logs.
How I check my logs ?
My code (MainActivity.kt
):
package com.example.paymentothetrip
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.activity_main)
var kmtotal = findViewById<EditText>(R.id.ikmtotal)
var ilitro = findViewById<EditText>(R.id.ilitro)
var ikmforlit = findViewById<EditText>(R.id.ikmforlit)
val btncalcular: TextView = findViewById<Button>(R.id.btncalcular)
btncalcular.setOnClickListener{
calcular(kmtotal, ikmforlit, ilitro, )
}
}
@SuppressLint("SetTextI18n")
private fun calcular(kmtotal: EditText, ikmforlit: EditText, ilitro: EditText) {
val txtresult = findViewById<TextView>(R.id.txtresult)
var result: Double = (kmtotal.text.toString().toDouble() / ikmforlit.text.toString().toDouble()) * ilitro.toString().toDouble()
txtresult.text = "R$ $result"
}
}
LOGCAT:
2022-02-26 21:16:10.655 19167-19167/com.example.paymentothetrip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paymentothetrip, PID: 19167
java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{28c5fa0 VFED..CL. ........ 0,745-1080,883 #7f0a01df app:id/ilitro aid=1073741825}"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.paymentothetrip.MainActivity.calcular(MainActivity.kt:32)
at com.example.paymentothetrip.MainActivity.onCreate$lambda-0(MainActivity.kt:24)
at com.example.paymentothetrip.MainActivity.$r8$lambda$x4PiI7hxLHE9UUw2OiqZu2i-kE4(Unknown Source:0)
at com.example.paymentothetrip.MainActivity$ExternalSyntheticLambda0.onClick(Unknown Source:8)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2022-02-26 21:16:10.670 19167-19167/com.example.paymentothetrip I/Process: Sending signal. PID: 19167 SIG: 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(只是解释如何跟踪错误 - 修复位于底部)
您的错误日志表明这是崩溃的原因:
NumberFormatException
通常是在以下情况下引起的 :您尝试将一些文本转换为数字,但您不能 - 它的格式不像可识别的数字。您尝试在此处将String
解析为Double
,因此字符串很可能包含无法读取为有效Double< 的文本/代码>。
该错误告诉您导致该错误的输入字符串:
这就是您调用
toDouble()
的原因。这显然是错误的,对吧?您期待类似1.234
或您输入的任何内容。该文本是对 AppCompatEditText 对象(即小部件本身)的引用。从该小部件获取文本内容时出现问题。因此,让我们看一下您从这些
EditText
中读取文本的代码,并尝试将它们解析为Double
:您看到其中哪一个是不同的吗?对于前两个,您要访问
kmtotal.text
和ikmforlit.text
并调用toString
来获取text
内容作为字符串。但对于最后一个,您没有访问其文本
- 您正在对EditText
本身调用toString()
。这就是为什么您在错误消息中获得对象的描述 - 这就是您尝试解析的字符串所以解决方案是您需要将其更改为
ilitro.text.toString()
ilitro.toString() 的。我只是想解释为什么会发生这种情况,以及如何使用错误日志来跟踪问题。它也告诉您问题出在哪里:这是您的代码中堆栈跟踪的第一行,这是您的代码在错误发生之前所做的最后一件事(
MainActivity)。这是开始寻找错误的好地方!
(Just explaining how you can track the bug down - the fix is at the bottom)
Your error log says this is the cause of the crash:
A
NumberFormatException
is usually caused when you try to convert some text into a number, but you can't - it's not formatted like a recognisable number. You're trying to parseString
s asDouble
s here, so there's a good chance a string contains text that can't be read as a validDouble
.The error tells you the input string that's causing it:
That's what you're calling
toDouble()
on. It's obviously wrong, right? You're expecting something like1.234
or whatever you entered. That text is a reference to anAppCompatEditText
object, the widget itself. Something's gone wrong with getting the text content from that widget.So let's look at your code where you read the text from those
EditText
s, and try to parse them asDouble
s:You see which one of those is different? For the first two, you're accessing
kmtotal.text
andikmforlit.text
and callingtoString
on that, to get thetext
contents as a string. But for the last one, you're not accessing itstext
- you're callingtoString()
on theEditText
itself. That's why you're getting that description of the object in the error message - that's the string you're trying to parseSo the solution is you need to change it to
ilitro.text.toString()
instead ofilitro.toString()
. I just wanted to explain why it's happening, and how you can use the error log to track issues down. It tells you where the problem is too:that's the first line of the stacktrace that's in your code, that's the last thing your code did before the error happened (line 32 of
MainActivity
). That's a good place to start looking for bugs!