kotlin - 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>
解决方案
要从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}"
推荐阅读
- javascript - 最新的 react-redux 和 redux-form 会导致严重的性能问题
- reactjs - TypeError:无法将 undefined 或 null 转换为对象需要澄清
- java - SSLHandshakeException: SSL handshake aborted: ssl=0xbe6af938: I/O error during system call, Connection reset by peer
- arrays - 如何在 React JS 中使用 Array 实现 Drop down 以获取以下代码片段
- javascript - 如何在 dom 元素的 extJS 显示字段中插入值
- sql - 如何在不合并的情况下加入不存在的行?
- google-drive-api - Google Drive API 使用 API Key 获取 AppProperties
- r - 如何仅考虑 r 中交易日期之前的最后 6 个月期间计算每个帐户的网络度量?
- nix - 直接从 nix-shell 启动 IDE
- azure-devops-rest-api - 如何通过提交检索构建?