我的Kotlin Android Studio应用程序在没有给出值的情况下按按钮时会崩溃

问题描述 投票:-1回答:1

它是一个模拟应用程序,显示有关任何给定数字的一些信息,例如其奇数或质数。

就像标题中所说的,当给出任何值时,应用程序都可以正常运行,但是如果我没有按此按钮,应用程序就会崩溃。我尝试了很多事情,但没有任何变化。这是代码:

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>

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

kotlin button crash
1个回答
1
投票

[不输入数字entry.text.toString()将返回""(一个空字符串),并且"".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()(如果您输入字母或小数,也可以防止崩溃),并按如下所示修改点击侦听器:

toIntOrNull()
© www.soinside.com 2019 - 2024. All rights reserved.