lateinit 属性 remindersViewModel 尚未初始化
lateinit property remindersViewModel has not been initialized
我在 activity 中使用 recyclerViews 显示两个片段。我正在尝试向回收站视图添加一个新项目,但我收到:“lateinit 属性 remindersViewModel 尚未初始化”错误。我已经在尝试在片段中初始化它。
我的片段 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)
}
}
})
}
}
我的Activity:
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()
}
}
我试图在 activity 中初始化视图模型,但我无法做到。
您拥有的每个 remindersViewModel
变量都是完全独立的,通过将它们标记为 lateinit
,您承诺在尝试读取它们之前为它们设置一个值。你在片段中这样做,但在调用 insert
之前你没有初始化 MainActivity
中的片段
谢谢,当我在 MainActivity 的 OnCreate 中初始化它时工作:
this.remindersViewModel = ViewModelProvider(this).get(RemindersViewModel::class.java)
我在 activity 中使用 recyclerViews 显示两个片段。我正在尝试向回收站视图添加一个新项目,但我收到:“lateinit 属性 remindersViewModel 尚未初始化”错误。我已经在尝试在片段中初始化它。
我的片段 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)
}
}
})
}
}
我的Activity:
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()
}
}
我试图在 activity 中初始化视图模型,但我无法做到。
您拥有的每个 remindersViewModel
变量都是完全独立的,通过将它们标记为 lateinit
,您承诺在尝试读取它们之前为它们设置一个值。你在片段中这样做,但在调用 insert
之前你没有初始化 MainActivity
中的片段
谢谢,当我在 MainActivity 的 OnCreate 中初始化它时工作:
this.remindersViewModel = ViewModelProvider(this).get(RemindersViewModel::class.java)