I am trying to build an application for my mini assignment, it is to create a temp converter tool. However, most of the codes out there are in java and since my assignment must be done in Kotlin. I must figure a way out myself.
This is my current code, and as you can see there are no errors, but when I click on the convert button, it will say my application crash.
Activity.kt
package edu.blabladmaoa.travelapp
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import org.w3c.dom.Text
class TempConverterActivity : AppCompatActivity() {
val TAG: String = "TempConverter"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_temp_converter)
val userinput = findViewById<EditText>(R.id.editTextTemp)
val optionFah = findViewById<RadioButton>(R.id.radioButtonFah)
val optionCel = findViewById<RadioButton>(R.id.radioButtonCel)
val convertbutton = findViewById<Button>(R.id.convertbutton)
val clearbutton = findViewById<Button>(R.id.clearbutton)
val results = findViewById<TextView>(R.id.tempresult)
// now the buttons are identified, i will set the each button to have their own listener
convertbutton.setOnClickListener{
if (optionFah.isChecked()){
val newinput = userinput.toString().toFloat()
results.setText(convertCelsiusToFahrenheit(newinput).toString())
}
else if (optionCel.isChecked()){
val newinput = userinput.toString().toFloat()
convertFahrenheitToCelsius(newinput)
results.setText(convertCelsiusToFahrenheit(newinput).toString())
}
}
clearbutton.setOnClickListener{
userinput.text.clear()
}
}
private fun convertFahrenheitToCelsius(fahrenheit: Float): Float {
return ((fahrenheit - 32) * 5 / 9)
}
private fun convertCelsiusToFahrenheit(celsius: Float): Float {
return ((celsius * 9) / 5) 32
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- i use "phone" to show numpad instead of normal keyboard-->
<EditText
android:id="@ id/editTextTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:hint="Enter temperature here" />
<RadioButton
android:id="@ id/radioButtonFah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="to Fahrenheit" />
<RadioButton
android:id="@ id/radioButtonCel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="to Celcius" />
<Button
android:id="@ id/convertbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Convert" />
<Button
android:id="@ id/clearbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear" />
<TextView
android:id="@ id/tempresult"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Android_Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.blabladmaoa.travelapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TravelApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TempConverterActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Expected Output
CodePudding user response:
instead of taking the text from the input you are calling toString()
on the EditText
itself
you need to change
val newinput = userinput.toString().toFloat()
to
val newinput = userinput.text.toString().toFloat()
Also as mentioned by Tenfour04 in the comments it will also crash if you press convert when you leave the EditText
empty
CodePudding user response:
val newinput = userinput.toString().toFloat()
Here you are trying to convert an EditText view to string which is not what you require.can we
Instead you want to get the text that is entered in that view and then convert it to the desired form which will be achieved by
val newInput = userInput.text.toString().toFloat()