Home > Back-end >  App crash when I press the button to sum two values
App crash when I press the button to sum two values

Time:02-28

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

CodePudding user response:

(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!

  • Related