j_md
j_md

Reputation: 27

Android app crashes on button press, Kotlin

I've created a android app that reverses a given string and outputs it. Originally, I create a prototype in Kotlin Playground, and I've got that version to work just fine, no problems. Since, I've moved the code and changed it slightly to fit within Android Studio and a GUI interface, however, since I've done this, every time I try to run the app, either with or without debugging, the app crashes every time the button is pressed.

I've looked around at some other answers of a similar nature but I haven't yet come across a solution. I've posted the MainActivity file and the activity_main file in this post.

I know that the code should work, as my version of it within Kotlin Playground runs with no issues, I just don't know what's causing the app to crash everytime the button is pressed.

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>

Upvotes: 1

Views: 523

Answers (1)

mightyWOZ
mightyWOZ

Reputation: 8355

To get the input text from an EditText object, you need to use the text property

When you write findViewById<EditText>(R.id.userInput), this gives you a reference to the EditText object, now if you want to get the text input then you have to use the text property as

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

You are incrementing the charPointerTwo, when you should increment the charPointerOne instead

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

You are only showing last reversed word to the user

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

Upvotes: 1

Related Questions