3

Android studio shows errors in the lines

REPOSITORY.insert(note){ onSuccess() }

in the "AddNewNoteFragmentViewModel" and in the lines

viewModel.insert(AppNote(name = name, text = text)){ view?.findNavController()?.navigate(R.id.action_addNewNoteFragment_to_mainFragment)}

in the file "AddNewNoteFragment"

I realized that the error is related to streams, but I do not know how to solve it

If anyone knows, please help, I could not find anything worthwhile on the Internet

AddNewNoteFragment

  package com.example.notes.fragments.add_new_note

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import com.example.notes.R
import com.example.notes.databinding.FragmentAddNewNoteBinding
import com.example.notes.model.AppNote
import com.example.notes.utilits.showToast


class AddNewNoteFragment : Fragment() {

    private var _binding: FragmentAddNewNoteBinding? = null
    val binding get() = _binding!!
    private lateinit var viewModel: AddNewNoteFragmentViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentAddNewNoteBinding.inflate(inflater, container, false)
        val view = binding.root
        return view

    }


    override fun onStart() {
        super.onStart()
        initialization()
    }

    private fun initialization() {
        viewModel = ViewModelProvider(this).get(AddNewNoteFragmentViewModel::class.java)

        binding.buttonAddNote.setOnClickListener {

            val name = binding.inputNameNote.text.toString()
            val text = binding.inputTextNote.text.toString()

            if (name.isEmpty()){

                showToast("Введите имя заметки")

            } else{

                viewModel.insert(AppNote(name = name, text = text)){
                    view?.findNavController()?.navigate(R.id.action_addNewNoteFragment_to_mainFragment)
                }

            }

        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

AddNewNoteFragmentViewModel

package com.example.notes.fragments.add_new_note

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.example.notes.model.AppNote
import com.example.notes.utilits.REPOSITORY
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class AddNewNoteFragmentViewModel(application: Application): AndroidViewModel(application) {

    fun insert(note: AppNote,onSuccess:()-> Unit) =
        viewModelScope.launch (Dispatchers.IO){
            REPOSITORY.insert(note){
                onSuccess()
            }
        }

}

Error screen

enter image description here

5
  • What is your RESPOSITORY? Can you include that code? Commented Mar 2, 2022 at 22:05
  • I think the problem is you are trying the navigate from fragment to other while the coroutine is still running by calling onSuccess at the mid of the coroutine process Commented Mar 2, 2022 at 23:53
  • add the navigation line as below. viewModelScope.launch (Dispatchers.Main){ view?.findNavController()?.navigate(R.id.action_addNewNoteFragment_to_mainFragment) } Commented Mar 3, 2022 at 9:50
  • Thanks to everyone who responded :) I solved the problem Commented Mar 3, 2022 at 9:53

2 Answers 2

6

Problem solved In AddNewNoteFragmentViewModel requires Dispatchers.IO to be changed to Dispatchers.Main

1
        Handler(Looper.getMainLooper()).postDelayed({
        val bundle = Bundle()
        bundle.putString("keyDashBoard", "No")
        findNavController().navigate(R.id.navigation_home_details,bundle)
    }, 6000)

You can add this in fragment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.