首页 > 解决方案 > 我的 android 应用程序在使用时崩溃,并且 Android Studio 显示“FATAL EXCEPTION”

问题描述

我最近一直在开发一个小费计算器安卓应用程序,今天我一直在尝试添加一种划分账单的方法。但是,我的应用程序因此在使用时崩溃。我一直试图弄清楚是什么导致它崩溃,但我似乎找不到任何东西。每当我现在使用我的应用程序时,都会收到此错误:

    Process: com.gradient.tiptime, PID: 6507
    java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{2d3edb1 VFED..CL. .F...... 600,60-1080,196 #7f0700d1 app:id/split_num 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.gradient.tiptime.MainActivity.calculateTip(MainActivity.kt:26)
        at com.gradient.tiptime.MainActivity$onCreate$1.onClick(MainActivity.kt:19)
        at android.view.View.performClick(View.java:7192)
        at android.view.View.performClickInternal(View.java:7166)
        at android.view.View.access$3500(View.java:824)
        at android.view.View$PerformClick.run(View.java:27592)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

这是我的“计算”按钮调用的函数:

fun calculateTip() {
        var mealCost: Double = cost_of_service.text.toString().toDouble()
        var splitNum: Double = split_num.toString().toDouble()

        val selectedId = tip_options.checkedRadioButtonId
        val tipPercentage = when(selectedId) {
            R.id.option_twenty_percent -> 0.20
            R.id.option_eighteen_percent -> 0.18
            else -> 0.15}

        //some values
        var tip = tipPercentage * mealCost
        val addedCost = tip + mealCost

        //finds the views
        val switch: Switch = findViewById(R.id.round_up_switch)
        val result: TextView = findViewById(R.id.tip_result)
        val total: TextView = findViewById(R.id.total_cost)
        val splitResult: TextView = findViewById(R.id.split_cost)
        val roundUp = switch.isChecked

        var split: Double = mealCost / splitNum
        var splitString: String = split.toString()

        //if roundup switch is checked, rounds tip up
        if (roundUp) {
            tip = kotlin.math.ceil(tip)}

        //formats the tip to $
        val formattedTip = NumberFormat.getCurrencyInstance().format(tip)
        val formattedCost = NumberFormat.getCurrencyInstance().format(addedCost)

        //changes the textViews in the app
        result.text = getString(R.string.tip_amount, formattedTip)
        total.text = getString(R.string.total_amount, formattedCost)
        splitResult.text = getString(R.string.total_amount, splitString)
    }

标签: androidandroid-studiokotlin

解决方案


从您提供的代码和错误中,错误看起来在行中

var splitNum: Double = split_num.toString().toDouble()

将此行更改为

var splitNum: Double = split_num.text.toString().toDouble()

这里的错误似乎是编译器无法将 EditText 字段转换为 Double 类型,这是不可能的,并导致转换错误。


推荐阅读