我的 Kotlin Android Studio 应用程序在未给出值时按下按钮时不断崩溃

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

这是一个简单的应用程序,可以显示有关任何给定数字的一些信息,例如它是奇数还是质数。

就像标题所说的那样,当给定任何值时,应用程序都可以正常工作,但是只要我在没有它的情况下按下按钮,应用程序就会崩溃。 我尝试了很多东西,但没有任何改变。这是代码:

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
        }
    }
}

这里是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>

我希望我的问题很清楚。我会回答任何问题。

不输入数字 entry.text.toString() 将 return ""(空字符串)和 "".toInt() 导致类似于或等于此的异常:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
  at java.lang.NumberFormatException.forInputString (NumberFormatException.java:65) 
  at java.lang.Integer.parseInt (Integer.java:592) 
  at java.lang.Integer.parseInt (Integer.java:615) 

我建议将 toInt() 替换为 toIntOrNull()(如果您输入字母或十进制数字,这也可以防止崩溃)并按如下方式修改您的点击监听器:

textButton?.setOnClickListener {
    textView.text = ""
    val num = entry.text.toString().toIntOrNull()
    val toPrint = if (num != null) {
        oddOrEven(num) + isPrime(num)
    } else {
        ""
    }
    textView.text = toPrint
}