1

I start doing a simple android app for practice to calculate the age with Kotlin programming language, when i click the button the app, can someone help me with this and fix it to know where is my wrong because I am just very beginner

package com.calcult.age.agecalcult

    import android.support.v7.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Toast
    import kotlinx.android.synthetic.main.activity_main.*
    import java.util.*

    class MainActivity : AppCompatActivity() {


        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var ageInput = ageText.text.toString()

            doIT.setOnClickListener{

                var currentYear = Calendar.getInstance().get(Calendar.YEAR)
                var getAge = currentYear-ageInput.toInt()
                Toast.makeText(this, getAge, Toast.LENGTH_LONG)
            }
        }

    }

XML FILE :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginEnd="148dp"
            app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="148dp"
            android:id="@+id/doIT"
            android:text="@string/button_text1"
            android:textSize="18sp"
            app:layout_constraintHorizontal_bias="0.0" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.771"/>
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/ageText" app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="215dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="85dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="84dp"
            android:layout_marginBottom="250dp" app:layout_constraintBottom_toTopOf="@+id/doIT"/>

</android.support.constraint.ConstraintLayout>
Mostafa
  • 159
  • 1
  • 11
  • 3
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Vladyslav Matviienko Dec 06 '18 at 12:02
  • you are getting the text from the edittext in `onCreate` before you enter something to it. Therefore it is empty, and it crashes when you are trying to convert empty string to int. – Vladyslav Matviienko Dec 06 '18 at 12:03
  • Can you check on the logs what is crashing the app. From the code it seems like age text is empty or it's a string not a number. You can change the inputType to number to be on the safe side. Also check the length of the string before converting and subtracting – Pradeep Pathak Dec 06 '18 at 12:10
  • share the logs for us to be of more help – Pradeep Pathak Dec 06 '18 at 12:11
  • logs doesn't give me any error – Mostafa Dec 06 '18 at 12:13

1 Answers1

2

As Vladyslav Matviienko said you're trying to get the text onCreate, what you need to do is something like this:

//Here the view is already created, so you'll be able to setup everything you need.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    setupListener()
}

//If you are learning, you should consider reading a bit about clean code, 
//it's always nice that a function does just one job, that's why I broke the code 
//below in two little functions. Have in mind, this code is not the best, but at the 
//moment i don't have much time to write this.

private fun setupListener() {
    doIT.setOnClickListener{

//Always be aware of the type of the variables, when i wrote the first answer i was 
//trying to do a math operatiton using a string, so it would not work. =P

        var currentYear = Calendar.getInstance().get(Calendar.YEAR)
        var getAge = (currentYear-getAge().toInt()).toString()
        Toast.makeText(this, getAge, Toast.LENGTH_LONG).show()
    }
}

private fun getAge(): String {
    return ageText.text.toString
}
  • you are returning the String from the getAge() function and than subtract from the current year. it will produce a compile time error. – Shubham Mittal Dec 06 '18 at 12:40
  • there are more problems with the code. After fixing this crash, there will show up at least one more in the line `Toast.makeText(this, getAge, Toast.LENGTH_LONG)`, as using `int` in Toast works totally different from `String`. Also the toast won't show, as there is no `.show()` – Vladyslav Matviienko Dec 06 '18 at 12:41
  • I have edited my answer, it's not perfect, but i'm at work now, feel free to adjust it. =) – Alexander Cavalheiro Becker Dec 06 '18 at 12:49
  • ok, looks almost perfect now. It would be perfect if you add some explanation on every change in original code you did. Basically our goal here is not to solve individual problem just by providing a working piece of code, but to make people understand what is wrong, and to prevent repeating of same problem. – Vladyslav Matviienko Dec 06 '18 at 12:52