Home > Mobile >  App keeps crashing and Logcat states "lateinit property todoAdapter has not been initialized&qu
App keeps crashing and Logcat states "lateinit property todoAdapter has not been initialized&qu

Time:06-20

This is the Main activity for a simple todo list app if you want to see more of the code then please let me know

When private lateinit var todoAdapter: TodoAdapter is removed app no longer crashes but the app will no longer show the todo item on the recycler view

"package com.example.todoit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.todoit.data.Todo
import com.example.todoit.data.TodoViewModel
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    private lateinit var todoAdapter: TodoAdapter
    private lateinit var todoViewModel: TodoViewModel

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


        rvTodoItems.layoutManager = LinearLayoutManager(this)
        rvTodoItems.adapter = todoAdapter

        btnAddTodo.setOnClickListener {

            val todoTitle = etTodoTitle.text.toString()
            if (todoTitle.isNotEmpty()) {
                val todo = Todo(0,todoTitle,false)
                etTodoTitle.text.clear()
                insertDataToDataBase()
                todoAdapter.addTodo(todo)
            }
        }
        btnDeleteTodo.setOnClickListener {
            todoAdapter.deleteDoneTodos()
        }
    }

    private fun insertDataToDataBase() {
        val todoTitle = etTodoTitle.text.toString()

        if(inputCheck(todoTitle)) {

            val todo = Todo(0, todoTitle, isChecked = false)
            //Add data to database
            todoViewModel.addTodoToDataBase(todo)
            Toast.makeText(this, "Successfully Added", Toast.LENGTH_LONG).show()
        }else{
            Toast.makeText(this, "Please fill out all fields", Toast.LENGTH_LONG).show()
            }
        }




    private fun inputCheck(todoTitle:String):Boolean {
        return !(TextUtils.isEmpty(todoTitle))
    }
}

CodePudding user response:

you forgot to initialize your todoAdapter, you need to initialize todoAdapter before calling it.

class MainActivity : AppCompatActivity() {
    private lateinit var todoAdapter: TodoAdapter
    private lateinit var todoViewModel: TodoViewModel

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

        todoAdapter = TodoAdapter() // if TodoAdapter requires parameters you need to pass them
        rvTodoItems.layoutManager = LinearLayoutManager(this)
        rvTodoItems.adapter = todoAdapter

        btnAddTodo.setOnClickListener {

            val todoTitle = etTodoTitle.text.toString()
            if (todoTitle.isNotEmpty()) {
                val todo = Todo(0,todoTitle,false)
                etTodoTitle.text.clear()
                insertDataToDataBase()
                todoAdapter.addTodo(todo)
            }
        }
        btnDeleteTodo.setOnClickListener {
            todoAdapter.deleteDoneTodos()
        }
    }

    private fun insertDataToDataBase() {
        val todoTitle = etTodoTitle.text.toString()

        if(inputCheck(todoTitle)) {

            val todo = Todo(0, todoTitle, isChecked = false)
            //Add data to database
            todoViewModel.addTodoToDataBase(todo)
            Toast.makeText(this, "Successfully Added", Toast.LENGTH_LONG).show()
        }else{
            Toast.makeText(this, "Please fill out all fields", Toast.LENGTH_LONG).show()
            }
        }




    private fun inputCheck(todoTitle:String):Boolean {
        return !(TextUtils.isEmpty(todoTitle))
    }
}

CodePudding user response:

The error clear your todoAdapter not initialzed you have to initialize before using like this :

todoAdapter=TodoAdapter(yourprams) 


        rvTodoItems.layoutManager = LinearLayoutManager(this)
        rvTodoItems.adapter = todoAdapter

        btnAddTodo.setOnClickListener {

            val todoTitle = etTodoTitle.text.toString()
            if (todoTitle.isNotEmpty()) {
                val todo = Todo(0,todoTitle,false)
                etTodoTitle.text.clear()
                insertDataToDataBase()
                todoAdapter.addTodo(todo)
            }
        }
        btnDeleteTodo.setOnClickListener {
            todoAdapter.deleteDoneTodos()
        }
    }
  • Related