skip to Main Content

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

expected output of what im trying to achieve

3

Answers


  1. 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

    Login or Signup to reply.
  2. val newinput = userinput.toString().toFloat()
    

    Here you are trying to convert an EditText object to a string using the toString() method. This will not give you the desired output.

    You can access the text attribute of the class EditText. This returns the content of the EditText view as a string. Please, see below.

    val newInput = userInput.text.toString().toFloat()
    
    Login or Signup to reply.
  3. You should check that your edittext isn’t empty:

    if(myEditText.getText != ""){
        //then convert
        //you should use edittext.text.tofloat
        // because the default value in edittext is String
        // and you can't convert string to float or int value 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search