方法setCurrentState必须在主线程、Android、Kotlin上调用

gj3fmq9x  于 2023-10-14  发布在  Android
关注(0)|答案(2)|浏览(125)

Android Studio显示行错误
REPOSITORY.insert(note){ onSuccess() }
在“AddNewNoteFragmentViewModel”和
viewModel.insert(AppNote(name = name, text = text)){ view?.findNavController()?.navigate(R.id.action_addNewNoteFragment_to_mainFragment)}
在文件“AddNewNoteFragment”中
我意识到这个错误与流有关,但我不知道如何解决它
如果有人知道,请帮助,我无法找到任何值得在互联网上

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()
            }
        }

}

错误页面

enter image description here

qcuzuvrc

qcuzuvrc1#

AddNewNoteFragmentViewModel中解决的问题需要将Dispatchers.IO更改为Dispatchers.Main

mwngjboj

mwngjboj2#

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

你可以在片段中添加它。

相关问题