So i was testing my app with my phone that i’ve created in android studio and whenever i use the calc() function that i’ve done in setOnClickListener(), when i press the button my phone exits the app and i get errors:
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)
here is my code
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()
}
}
}
activity_main.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>
i tried commenting var calculation and everything related to calc() function and indeed it worked but i still need to use this function.
if this can help, i am using nougat 7.0
2
Answers
The number format is wrong, you can found the cause in exception description:
The string is empty, so you have to search why it happened.
At start you get a value from text field and remember it in vi variable. At start text field’s value is empty string. In the click handler you try to convert this remembered empty string to double and got the error.
You have to get value of text field in the click handler, something like this:
It’s because your vi and/or height fields are empty. Try like this: