0

我正在使用 recyclerViews 在活动中显示两个片段。我正在尝试向回收站视图添加一个新项目,但我得到:“lateinit 属性提醒视图模型尚未初始化”错误。我已经在尝试在片段中初始化它。

我的片段1:


import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.bubblereminder.room.Reminders
import com.example.bubblereminder.room.RemindersListAdapter
import com.example.bubblereminder.room.RemindersViewModel

class ScheduledFragment : Fragment() {
    private lateinit var reminderViewModel: RemindersViewModel

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

        val v = inflater.inflate(R.layout.fragment_scheduled, container, false)

        return v
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val recyclerView = view?.findViewById<RecyclerView>(R.id.recycler_scheduled)
        val adapter = context?.let { RemindersListAdapter(it) }
        if (recyclerView != null) {
            recyclerView.adapter = adapter
        }
        if (recyclerView != null) {
            recyclerView.layoutManager = LinearLayoutManager(context)
        }

        this.reminderViewModel = ViewModelProvider(this).get(RemindersViewModel::class.java)
        reminderViewModel.allReminders.observe(viewLifecycleOwner, Observer { reminders ->
            // Update the cached copy of the words in the adapter.
            reminders?.let {
                if (adapter != null) {
                    adapter.setReminders(it)
                }
            }
        })
    }
}

我的片段2:

    package com.example.bubblereminder

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.bubblereminder.room.Reminders
import com.example.bubblereminder.room.RemindersListAdapter
import com.example.bubblereminder.room.RemindersViewModel

class DoneFragment : Fragment() {
    private lateinit var reminderViewModel: RemindersViewModel

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

        val v = inflater.inflate(R.layout.fragment_scheduled, container, false)

        return v
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val recyclerView = view?.findViewById<RecyclerView>(R.id.recycler_scheduled)
        val adapter = context?.let { RemindersListAdapter(it) }
        if (recyclerView != null) {
            recyclerView.adapter = adapter
        }
        if (recyclerView != null) {
            recyclerView.layoutManager = LinearLayoutManager(context)
        }

        this.reminderViewModel = ViewModelProvider(this).get(RemindersViewModel::class.java)
        reminderViewModel.allReminders.observe(viewLifecycleOwner, Observer { reminders ->
            // Update the cached copy of the words in the adapter.
            reminders?.let {
                if (adapter != null) {
                    adapter.setReminders(it)
                }
            }
        })
    }
}

我的活动:

    package com.example.bubblereminder

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.FrameLayout
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.bubblereminder.room.Reminders
import com.example.bubblereminder.room.RemindersDao
import com.example.bubblereminder.room.RemindersListAdapter
import com.example.bubblereminder.room.RemindersViewModel
import com.ramotion.circlemenu.CircleMenuView

class MainActivity : AppCompatActivity() {
    private lateinit var remindersViewModel: RemindersViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupMenuEventListener()
        setupFragments(R.id.fragment_container1, ScheduledFragment())
        setupFragments(R.id.fragment_container2, DoneFragment())
    }

    private fun setupMenuEventListener() {
        val circleMenu = findViewById<CircleMenuView>(R.id.circle_menu)
        circleMenu.eventListener = object : CircleMenuView.EventListener() {

            override fun onButtonClickAnimationEnd(view: CircleMenuView, index: Int) {
                when (index) {
                    0 -> {
                        remindersViewModel.insert(Reminders(0,"Sample Reminder Text"))
                        val confirmToast = "Task 1 Finished"
                        Toast.makeText(this@MainActivity, confirmToast, Toast.LENGTH_SHORT)
                            .show()
                    }
                    1 -> {
                        val confirmToast = "Task 2 Finished"
                        Toast.makeText(this@MainActivity, confirmToast, Toast.LENGTH_SHORT)
                            .show()
                    }
                    2 -> {
                        val confirmToast = "Task 3 Finished"
                        Toast.makeText(this@MainActivity, confirmToast, Toast.LENGTH_SHORT)
                            .show()
                    }
                    3 -> {
                        val confirmToast = "Task 4 Finished"
                        Toast.makeText(this@MainActivity, confirmToast, Toast.LENGTH_SHORT)
                            .show()
                    }
                    4 -> {
                        val confirmToast = "Task 5 Finished"
                        Toast.makeText(this@MainActivity, confirmToast, Toast.LENGTH_SHORT)
                            .show()
                    }
                }
            }
        }
    }
    private fun setupFragments(id:Int , frag:Fragment){
        var fragmentManager = supportFragmentManager
        fragmentManager.beginTransaction()
            .replace(id, frag)
            .commit()
    }
}

我试图在活动中初始化视图模型,但我无法做到。

4

2 回答 2

0

谢谢,当我在 MainActivity 的 OnCreate 初始化它时工作:

this.remindersViewModel = ViewModelProvider(this).get(RemindersViewModel::class.java)
于 2020-09-23T13:47:34.217 回答
0

您拥有的每个remindersViewModel变量都是完全独立的,并且通过将它们标记为lateinit您承诺在尝试读取它们之前设置它们的值。您正在片段中执行此操作,但在MainActivity调用它之前您没有初始化insert

于 2020-09-23T13:33:23.643 回答