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

标签 kotlin button crash

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

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

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()将返回""(一个空字符串),并且"".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
}

关于kotlin - 我的Kotlin Android Studio应用程序在没有给出值的情况下按按钮时会继续崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59690025/

相关文章:

cocoa - Core Data 内部方法崩溃 (SIGSEGV)

kotlin - 如何在 tornadoFx 中注入(inject) ItemViewModel

Kotlin 类型定义

iphone - 如果我的应用崩溃了,我该如何保存在userdefaults中

javascript - 单击按钮时如何更改按钮中的类

java - 整个窗口的按钮 - Java

android - USB 附件的接收处理程序中出现空指针异常

android - 在普通函数中调用 .enqueue 还是在 kotlin 挂起函数中调用 .execute 更好?

android - 为什么 Jetpack Compose Preview 什么也不显示?

swift - 在 tvOS 上的 SwiftUI 中更改按钮的颜色