Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

565
Visualizações
Kotlin - interface not initialized when passing data between fragments

I am new to android development and making a simple To-do list app to get my bearings.

I have a fragment from my MainActivity that contains a list, called FragToDoList. I also have a class DialogAddToDo that extends DialogFragment to bring up a dialog to input your to do list item infomation. I am having trouble passing the data entered into DialogAddTodo back to the FragToDoList class to then display it in the list.

It is saying my interface is not initialized, and cant find a way to resolve this issue. Does anyone have some pointers?

class FragToDoList : Fragment(), DialogAddTodo.ToDoAddedListener {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null


     private val toDoList: MutableList<ToDoItem>? = null
     lateinit var adapter: ToDoAdapter
    private lateinit var listViewItem: ListView



   lateinit var mContext: Context
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view: View = inflater.inflate(R.layout.fragment_frag__to_do_list, container, false)
        listViewItem = view.findViewById(R.id.listView)
        //for showing items in list view
//        adapter = ToDoAdapter(mContext, toDoList!!)
//        listViewItem.adapter = adapter

        val fab = view.findViewById<FloatingActionButton>(R.id.fabAddTask)
        fab?.setOnClickListener {
            showAddTaskDialog(childFragmentManager);

        }
        // Inflate the layout for this fragment
        return view
    }

    private fun showAddTaskDialog(childFragmentManager:FragmentManager) {
        var dialog = DialogAddTodo()
        dialog.show(childFragmentManager,DialogAddTodo.TAG)
    }


    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment FragTodoList.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            FragToDoList().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun onToDoAdded(item: ToDoItem) {
        toast("added",mContext)
    }
}

class DialogAddTodo : DialogFragment() {

    interface ToDoAddedListener {
        fun onToDoAdded(item: ToDoItem)
    }

    private lateinit var onTodoAdded: ToDoAddedListener
    private val todoItemModel: TodoItemModel? =null

    companion object {
        const val TAG = "Dialog Add Task"
    }

    lateinit var mContext: Context

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view: View = inflater.inflate(R.layout.dialog_add_task, container, false)

        var addBut = view.findViewById<Button>(R.id.butAddTaskDialog)
        addBut.setOnClickListener()
        {
            val todoItemData = ToDoItem.createToDoItem()

            var taskNameText =
                view.findViewById<TextView>(R.id.editTextTaskName)

            if (taskNameText != null)
                todoItemData.itemDataText = taskNameText?.text.toString()
            todoItemData.done = false

            todoItemData.UID = getRandomString(Int.SIZE_BITS - 1)

//            Frag_ToDoList().adapter.notifyDataSetChanged()
            //b.setItem(todoItemData)
            //todoItemModel.setItem(todoItemData)

            onTodoAdded.onToDoAdded(todoItemData)
            this.dismiss()

        }

        return view
    }


}

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You declared lateinit variable

private lateinit var onTodoAdded: ToDoAddedListener

however it remains uninitialized.

When uninitialized var accessed it throws an exception.

You need to assign variable for example using accessor:

var dialog = DialogAddTodo()

// Initialize listener
dialog.setListener(this)

dialog.show(childFragmentManager,DialogAddTodo.TAG)

In DialogAddTodo add following method:

fun setListener(onTodoAdded: ToDoAddedListener) {
    this.onTodoAdded = onTodoAdded
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda