Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7199

My Kotlin Android Studio App Keeps Crashing When Pushing Button While Value Is Not Given

$
0
0

Its a simle app that shows some information about any given number like if its odd or prime.

Like the title said app works fine when any value is given but as soon as i push buton without it app crashes. I have tried many thing but nothing changes. Here is code:

package com.example.numbers

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


fun oddOrEven(num: Int): String
{
    return when (num % 2 == 0)
    {
        true -> "Number is: even\n"
        false -> "Number is: odd\n"
    }
}

fun isPrime(num: Int): String
{
        var flag = false
        for (i in 2..num / 2 + 1)
        {
            if (num % i == 0)
            {
                flag = true
                break
            }
        }
        return when (flag)
        {
            false ->  "Number is: prime\n"
            true ->  "Number is: not prime\n"
        }
}



class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val textButton = findViewById<Button>(R.id.printButton)

        val entry = findViewById<TextView>(R.id.entry)

        val textView = findViewById<TextView>(R.id.textView)


        textButton?.setOnClickListener {
            textView.text = ""
            val num = entry.text.toString().toInt()
            var toPrint = ""


            toPrint += oddOrEven(num) + isPrime(num)

            textView.text = toPrint
        }
    }
}

Here is XML

<TextView
        android:id="@+id/text1"
        android:layout_width="266dp"
        android:layout_height="62dp"
        android:layout_marginTop="28dp"
        android:gravity="center"
        android:text="Podaj liczbę"
        android:textSize="36sp"
        app:fontFamily="@font/anonymous_pro"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/entry"
        android:layout_width="291dp"
        android:layout_height="57dp"
        android:ems="10"
        android:gravity="center"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text1" />

    <Button
        android:id="@+id/printButton"
        android:layout_width="180dp"
        android:layout_height="42dp"
        android:layout_marginTop="36dp"
        android:text="Ok"
        android:textColor="#000000"
        android:textColorHint="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/entry" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="400dp"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="52dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/printButton"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

I hope my problem is clear. I will answer any question.


Viewing all articles
Browse latest Browse all 7199

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>