Search code examples
androidxmlkotlin

Why does my app not stay opened, when I run it on my Phone?


So I am making an App for a friend and It's my first time using Android Studio and Kotlin in general so I'm really inexperienced. I have three codes. The main kt being:

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.erstesachenso.MyAdapter
import com.example.erstesachenso.R


class MainActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: MyAdapter
    private lateinit var layoutManager: LinearLayoutManager

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

        // Finde die Views aus dem Layout
        val editTextColumnName = findViewById<EditText>(R.id.editTextColumnName)
        val buttonAddColumn = findViewById<Button>(R.id.buttonAddColumn)

        // RecyclerView und Adapter initialisieren
        recyclerView = findViewById(R.id.recyclerView)
        layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        adapter = MyAdapter()
        recyclerView.adapter = adapter

        // Klick-Listener für den Button hinzufügen
        buttonAddColumn.setOnClickListener {
            val columnName = editTextColumnName.text.toString().trim()
            if (columnName.isNotEmpty()) {
                // Spalte hinzufügen
                adapter.addColumn(columnName)
                // Feld leeren
                editTextColumnName.text.clear()
            }
        }
    }
}

Then I have a MyAdapter kt:

package com.example.erstesachenso

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    private val columns = mutableListOf<String>()

    override fun onBindViewHolder(holder: ViewHolder, position: Int){
        val columnName = columns[position]
        holder.bind(columnName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        TODO("Not yet implemented")
    }

    override fun getItemCount(): Int {
        return columns.size
    }

    fun addColumn(columnName: String) {
        TODO("Not yet implemented")
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val textViewColumn: TextView = itemView.findViewById(android.R.id.text1)

        fun bind(columnName: String) {
            textViewColumn.text = columnName
        }
    }
}

and an activity_main xml being:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">


    <EditText
        android:id="@+id/editTextColumnName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Spaltenname eingeben"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:maxLines="1" />

    <Button
        android:id="@+id/buttonAddColumn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Spalte hinzufügen"
        android:layout_gravity="center_horizontal" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:orientation="vertical" />

</LinearLayout>

So when I run the app (I tried it on two phones it worked on neither one) It opens up for a split second and immediatly closes again. I have the minimum SDK to Android 7 and I have installed Android 10 to 13.

I asked Chat gpt what to do since I just started today and no one I know is into coding. I also reaserched but the only thing I got is that my code is probably faulty. But I don't know what exactly.


Solution

  • When the application reads the line TODO("Not yet implemented") it will produce an error.

    If you check your Logcat you might see this error.

    Process: com.example.myapplication, PID: 1088
    kotlin.NotImplementedError: An operation is not implemented: Not yet implemented
    

    I would suggest you to implement the following methods in MyAdapter.kt.

    1. onCreateViewHolder()

    From:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            TODO("Not yet implemented")
    }
    

    To:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val viewHolder = ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.your_item_layout, parent, false))
        return viewHolder
    }
    

    Note: R.layout.your_item_layout is the layout that recycler view uses. You should provide your item layout id in the inflate() method instead.

    1. addColumn()
    fun addColumn(columnName: String) {
        TODO("Not yet implemented")
    }