Android Studio在setOnClickListenerKotlin中使用函数时出错?

gudnpqoy  于 2023-03-03  发布在  Kotlin
关注(0)|答案(2)|浏览(128)

因此,我在Android Studio中创建的手机上测试我的应用程序,每当我使用setOnClickListener()中的calc()函数时,当我按下按钮时,手机退出应用程序,并收到错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fallingobject, PID: 20895
    java.lang.NumberFormatException: empty String
        at jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
        at jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
        at java.lang.Double.parseDouble(Double.java:543)
        at com.example.fallingobject.MainActivity.onCreate$lambda$0(MainActivity.kt:32)
        at com.example.fallingobject.MainActivity.$r8$lambda$_3dgtv8ZNTl9g4zWX7J50i2eoew(Unknown Source:0)
        at com.example.fallingobject.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
        at android.view.View.performClick(View.java:7498)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1202)
        at android.view.View.performClickInternal(View.java:7471)
        at android.view.View.access$3700(View.java:843)
        at android.view.View$PerformClick.run(View.java:29071)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:233)
        at android.os.Looper.loop(Looper.java:344)
        at android.app.ActivityThread.main(ActivityThread.java:8205)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071)

这是我代码
MainActivity.kt:

package com.example.fallingobject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import kotlin.math.sqrt

fun calc(vi: Double, height: Double): String {
    val a = 9.8

    val vf = sqrt((vi * vi) + 2 * a * height)
    val va = (vf + vi) / 2
    val t = height / va
    return t.toString()
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val calcBtn = findViewById<Button>(R.id.calcBtn)
        val vi = findViewById<EditText>(R.id.sIn).text.toString()
        val height = findViewById<EditText>(R.id.hIn).text.toString()
        calcBtn.setOnClickListener {
            val calculation = calc(vi.toDouble(), height.toDouble())
            //Toast.makeText(this@MainActivity, calculation, Toast.LENGTH_SHORT).show()
        }
    }
}

活动_主要. xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#131313"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/sIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="100dp"
        android:layout_marginEnd="100dp"
        android:layout_marginBottom="55dp"
        android:backgroundTint="#BE40EC"
        android:ems="10"
        android:inputType="number"
        android:textColor="#673AB7"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/hIn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/hIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="200dp"
        android:layout_marginEnd="100dp"
        android:layout_marginBottom="120dp"
        android:backgroundTint="#FFBE40EC"
        android:ems="10"
        android:inputType="number"
        android:textColor="#673AB7"
        app:layout_constraintBottom_toTopOf="@+id/calcBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/calcBtn"
        android:layout_width="153dp"
        android:layout_height="56dp"
        android:layout_marginStart="100dp"
        android:layout_marginTop="364dp"
        android:layout_marginEnd="100dp"
        android:layout_marginBottom="80dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/editTextNumber3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextNumber3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="500dp"
        android:layout_marginEnd="100dp"
        android:layout_marginBottom="186dp"
        android:backgroundTint="#FFBE40EC"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="73dp"
        android:layout_height="33dp"
        android:layout_marginStart="100dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="100dp"
        android:layout_marginBottom="31dp"
        android:text="Speed"
        android:textColor="#BE40EC"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/sIn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="150dp"
        android:layout_marginEnd="100dp"
        android:layout_marginBottom="16dp"
        android:text="Height"
        android:textColor="#BE40EC"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/hIn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我试着注解var计算和所有与calc()函数相关的东西,它确实工作了,但我仍然需要使用这个函数。
如果这可以帮助,我正在使用牛轧糖7. 0

p8ekf7hl

p8ekf7hl1#

数字格式错误,请在异常描述中查找原因:
java.lang.NumberFormatException:空字符串
字符串是空的,所以你必须搜索它发生的原因。

...
val vi = findViewById<EditText>(R.id.sIn).text.toString()
...
calcBtn.setOnClickListener {
   val calculation = calc(vi.toDouble(), height.toDouble())
}

在开始时你从文本字段中获取一个值并将其保存在vi变量中。在开始时文本字段的值是空字符串。在click处理程序中你试图将这个保存的空字符串转换为double,但得到了错误。
你必须在click处理程序中获取文本字段的值,类似于:

val calcBtn = findViewById<Button>(R.id.calcBtn)
val vfield = findViewById<EditText>(R.id.sIn)
val hfield = findViewById<EditText>(R.id.hIn)
calcBtn.setOnClickListener {
    val vi = vfield.text.toString()
    val height = hfield.text.toString()
    val calculation = calc(vi.toDouble(), height.toDouble())            
}
zkure5ic

zkure5ic2#

这是因为您的 vi 和/或 height 字段为空。请尝试以下操作:

calcBtn.setOnClickListener {
    if (vi.isNotEmpty() && height.isNotEmpty()) {
        val calculation = calc(vi.toDouble(), height.toDouble())
    }
}

相关问题