当我按下按钮对两个值求和时应用程序崩溃

发布于 2025-01-10 05:55:37 字数 3247 浏览 0 评论 0原文

当我尝试按下按钮对编辑文本求和时,我的应用程序崩溃了,但我不知道如何修复,因为我看不到应用程序日志。 我如何检查我的日志?

我的代码(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2025-01-17 05:55:37

(只是解释如何跟踪错误 - 修复位于底部)

您的错误日志表明这是崩溃的原因:

java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{28c5fa0 VFED..CL. ........ 0,745-1080,883 #7f0a01df app:id/ilitro aid=1073741825}"

NumberFormatException 通常是在以下情况下引起的 :您尝试将一些文本转换为数字,但您不能 - 它的格式不像可识别的数字。您尝试在此处将 String 解析为 Double,因此字符串很可能包含无法读取为有效 Double< 的文本/代码>。

该错误告诉您导致该错误的输入字符串:

"androidx.appcompat.widget.AppCompatEditText{28c5fa0 VFED..CL. ........ 0,745-1080,883 #7f0a01df app:id/ilitro aid=1073741825}"

这就是您调用 toDouble() 的原因。这显然是错误的,对吧?您期待类似 1.234 或您输入的任何内容。该文本是对 AppCompatEditText 对象(即小部件本身)的引用。从该小部件获取文本内容时出现问题。

因此,让我们看一下您从这些 EditText 中读取文本的代码,并尝试将它们解析为 Double

var result: Double = (kmtotal.text.toString().toDouble() / ikmforlit.text.toString().toDouble())
    * ilitro.toString().toDouble()

您看到其中哪一个是不同的吗?对于前两个,您要访问 kmtotal.textikmforlit.text 并调用 toString 来获取 text 内容作为字符串。但对于最后一个,您没有访问其文本 - 您正在对EditText本身调用toString()。这就是为什么您在错误消息中获得对象的描述 - 这就是您尝试解析的字符串


所以解决方案是您需要将其更改为 ilitro.text.toString() ilitro.toString() 的。我只是想解释为什么会发生这种情况,以及如何使用错误日志来跟踪问题。它也告诉您问题出在哪里:

at com.example.paymentothetrip.MainActivity.calcular(MainActivity.kt:32)

这是您的代码中堆栈跟踪的第一行,这是您的代码在错误发生之前所做的最后一件事(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:

java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{28c5fa0 VFED..CL. ........ 0,745-1080,883 #7f0a01df app:id/ilitro aid=1073741825}"

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 parse Strings as Doubles here, so there's a good chance a string contains text that can't be read as a valid Double.

The error tells you the input string that's causing it:

"androidx.appcompat.widget.AppCompatEditText{28c5fa0 VFED..CL. ........ 0,745-1080,883 #7f0a01df app:id/ilitro aid=1073741825}"

That's what you're calling toDouble() on. It's obviously wrong, right? You're expecting something like 1.234 or whatever you entered. That text is a reference to an AppCompatEditText 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 EditTexts, and try to parse them as Doubles:

var result: Double = (kmtotal.text.toString().toDouble() / ikmforlit.text.toString().toDouble())
    * ilitro.toString().toDouble()

You see which one of those is different? For the first two, you're accessing kmtotal.text and ikmforlit.text and calling toString on that, to get the text contents as a string. But for the last one, you're not accessing its text - you're calling toString() on the EditTextitself. That's why you're getting that description of the object in the error message - that's the string you're trying to parse


So the solution is you need to change it to ilitro.text.toString() instead of ilitro.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:

at com.example.paymentothetrip.MainActivity.calcular(MainActivity.kt:32)

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文