i'm learning kotlin and i'm trying to make a program that calculates the average speed. my program crashes trying to calculate the average speed in kotlin and i have no idea why it is giving this error and how to fix it.
the error that appears is: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.activityspeed, PID: 10600 kotlin.UninitializedPropertyAccessException: lateinit property etT0 has not been initialized at com.example.activityspeed.MainActivity.getEtT0(MainActivity.kt:18) at com.example.activityspeed.MainActivity.onClick(MainActivity.kt:44) 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) I/Process: Sending signal. PID: 10600 SIG: 9 Disconnected from the target VM, address: 'localhost:58784', transport: 'socket'
Kotlin:
package com.example.atividadevelocidade
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatEditText
import kotlin.math.pow
class MainActivity : AppCompatActivity(), View.OnClickListener {
lateinit var btCalcularVelo: AppCompatButton
lateinit var etV1: AppCompatEditText
lateinit var etV0: AppCompatEditText
lateinit var etT1: AppCompatEditText
lateinit var etT0: AppCompatEditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
etV1 = findViewById(R.id.etV1)
etV0 = findViewById(R.id.etV0)
btCalcularVelo = findViewById(R.id.btCalcularVelo)
btCalcularVelo.setOnClickListener(this)
}
private fun calcularVelo(v1: Double, v0: Double, t1: Double, t0: Double) : Double{
if (v1 > 0.0 && v0 >= 0.0 && t1 > 0.0 && t0 >= 0.0) {
return (((v1 - v0) / (t1 - t0)))
} else {
throw IllegalArgumentException("Ta Errado.")
}
}
override fun onClick(v: View?) {
if(v!!.id == R.id.btCalcularVelo) {
val v0 = etV0.text.toString().toDouble()
val v1 = etV1.text.toString().toDouble()
val t0 = etT0.text.toString().toDouble()
val t1 = etT1.text.toString().toDouble()
val resultado = calcularVelo(v1, v0, t1, t0)
Toast.makeText(this, "A velocidade media é: $resultado", Toast.LENGTH_LONG)
.show()
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:gravity="center"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Caluclador de Velocidade media"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvV0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitulo"
android:text="Velocidade Inicial:"
android:layout_marginTop="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etV0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvV0"
android:scaleX="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etV0"
android:text="Velocidade Final:"
android:layout_marginTop="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvV1"
android:scaleX="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etV1"
android:text="Tempo Final:"
android:layout_marginTop="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvT1"
android:scaleX="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvT0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etT1"
android:text="Tempo Inicial:"
android:layout_marginTop="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etT0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvT0"
android:scaleX="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btCalcularVelo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etT0"
android:text="Calcular"
android:layout_marginTop="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
1条答案
按热度按时间zbq4xfa01#
根据您得到这个错误:
属性etT0尚未在com.example.activityspeed.MainActivity.getEtT0(MainActivity.kt:18)上初始化,位于
和你的准则。
变量“etT0”和“etT1”未在onCreate函数中初始化。
就像你对“etV1”和“etV0”所做的那样。
添加以下两行:
你的onCreate乐趣应该是这样的: