首页 > 解决方案 > Android应用程序在按下按钮时崩溃,Kotlin

问题描述

我创建了一个 android 应用程序,它可以反转给定的字符串并输出它。最初,我在 Kotlin Playground 中创建了一个原型,并且该版本可以正常工作,没有问题。因为,我已经移动了代码并稍微改变了它以适应 Android Studio 和 GUI 界面,但是,因为我已经这样做了,每次我尝试运行应用程序时,无论是否调试,应用程序都会崩溃按下按钮的时间。

我已经查看了其他一些类似性质的答案,但我还没有找到解决方案。我在这篇文章中发布了 MainActivity 文件和 activity_main 文件。

我知道代码应该可以工作,因为我在 Kotlin Playground 中的版本运行没有问题,我只是不知道每次按下按钮时导致应用程序崩溃的原因。

MainActivity.kt

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

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

        val button = findViewById<Button>(R.id.useButton)

        button.setOnClickListener {
            val userInput = findViewById<EditText>(R.id.userInput).toString()
            val exclusion = findViewById<EditText>(R.id.restrictedChars).toString()
            val programOutput = findViewById<TextView>(R.id.programOutput)

            val wordsInString = userInput.split(" ")
            val wordsSize = wordsInString.size
            var wordPointer = 0

            while (wordPointer < wordsSize) {
                val currentWord = wordsInString[wordPointer]
                val charArray = currentWord.toCharArray()

                var charPointerOne = 0
                var charPointerTwo = currentWord.length - 1

                while (charPointerOne < charPointerTwo) {
                    if (exclusion.contains(charArray[charPointerOne])) {
                        charPointerTwo++
                    } else if (exclusion.contains(charArray[charPointerTwo])) {
                        charPointerTwo--
                    } else {
                        val charToSwitchOne = charArray[charPointerOne]
                        val charToSwitchTwo = charArray[charPointerTwo]
                        charArray[charPointerOne] = charToSwitchTwo
                        charArray[charPointerTwo] = charToSwitchOne
                        charPointerOne++
                        charPointerTwo--
                    }
                }

                wordPointer++
                val outputString = String(charArray)
                programOutput.text = outputString
            }
        }
    }
}

activity_main.xml

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

    <Button
        android:id="@+id/useButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="158dp"
        android:layout_marginTop="22dp"
        android:layout_marginEnd="159dp"
        android:layout_marginBottom="275dp"
        android:text="Reverse String"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/programOutput" />

    <EditText
        android:id="@+id/userInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="204dp"
        android:layout_marginEnd="101dp"
        android:layout_marginBottom="2dp"
        android:ems="10"
        android:hint="Enter a Word or String"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/restrictedChars"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/programOutput"
        android:layout_width="210dp"
        android:layout_height="42dp"
        android:layout_marginStart="100dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="101dp"
        android:layout_marginBottom="13dp"
        android:text=""
        app:layout_constraintBottom_toTopOf="@+id/useButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/userInput"
        app:layout_constraintVertical_bias="0.75" />

    <EditText
        android:id="@+id/restrictedChars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="13dp"
        android:layout_marginEnd="101dp"
        android:layout_marginBottom="37dp"
        android:ems="10"
        android:hint="Enter Restrictions"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/programOutput"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/userInput" />
</androidx.constraintlayout.widget.ConstraintLayout>

标签: kotlin

解决方案


要从EditText对象中获取输入文本,您需要使用text属性

当您编写时findViewById<EditText>(R.id.userInput),这会为您提供对该EditText对象的引用,现在如果您想获取文本输入,那么您必须使用 text 属性作为

val userInput = findViewById<EditText>(R.id.userInput).text.toString()
val exclusion = findViewById<EditText>(R.id.restrictedChars).text.toString()

你正在增加charPointerTwo, 当你应该charPointerOne增加

if (exclusion.contains(charArray[charPointerOne])) {
    charPointerTwo++     // change this to charPointerOne++
}

您只向用户显示最后一个反转的单词

// Following code sets the current reversed word as text on programOutput
wordPointer++
val outputString = String(charArray)
programOutput.text = outputString

// to fix this change the last line to
programOutput.text = "$outputString${programOutput.text}"

推荐阅读