0

我有一个大部分时间都会启动的应用程序,但每 7 次左右启动它就会崩溃并出现错误:

kotlin.UninitializedPropertyAccessException:lateinit 属性 weekdayList 尚未初始化

这是一个明显的错误,我只是不确定如何确保在我的应用程序上下文中足够早地初始化变量。

我尝试过的事情

  • 我尝试移动变量,使“内部”和“外部”变量,一个内部变量onCreate和一个下划线变量作为类变量。

  • 更改视图模型,使其等到对数据库的调用完成(我无法完成这项工作,但主要是因为我不知道该怎么做)。

我认为问题出在onCreate函数中,并且工作日观察没有比weekdayList调用任务观察(需要变量的地方)更快地设置变量的值?


编辑 1

我引用了这个,但我最终遇到了类似的错误

java.lang.IndexOutOfBoundsException:空列表不包含索引 1 处的元素。


编辑 2

我现在了解lateinit变量和可空对象是如何工作的,我想尝试更好地澄清这一点。

在我点击for之前,weekdayList 需要将变量初始化为正确的列表,否则应用程序将崩溃。observetaskList

我尝试将变量设置为可为空,并最终得到:

  • 当程序为空时跳过部分程序(不是一个选项)
  • 因空指针异常而崩溃(如果设置为不可为空)
  • 没有任务被分配到任何一天,这意味着没有更新回收站视图,从而使应用程序看起来不包含任何任务。
  • 工作日按钮不起作用,因为没有weekdayList可供他们比较以启动下一个活动

我的问题不在于弄清楚它是否null存在,它试图保证它不会是null.

对困惑感到抱歉


主要活动

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private val plannerViewModel: PlannerViewModel by viewModels {
        PlannerViewModelFactory((application as PlannerApplication).repository)
    }

    private var weekdayList: List<Weekday> = listOf()
    private var taskList: List<Task> = listOf()
    private var taskDayList = mutableListOf<Task>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val clearButtonText = binding.clearCardText
        val sundayButtonText = binding.sundayCardText
        val mondayButtonText = binding.mondayCardText
        val tuesdayButtonText = binding.tuesdayCardText
        val wednesdayButtonText = binding.wednesdayCardText
        val thursdayButtonText = binding.thursdayCardText
        val fridayButtonText = binding.fridayCardText
        val saturdayButtonText = binding.saturdayCardText
        val sundayRv: RecyclerView = binding.sundayRv
        val sundayAdapter = TaskRvAdapter(null)
        sundayRv.adapter = sundayAdapter
        sundayRv.layoutManager = LinearLayoutManager(this)
        val mondayRv: RecyclerView = binding.mondayRv
        val mondayAdapter = TaskRvAdapter(null)
        mondayRv.adapter = mondayAdapter
        mondayRv.layoutManager = LinearLayoutManager(this)
        val tuesdayRv: RecyclerView = binding.tuesdayRv
        val tuesdayAdapter = TaskRvAdapter(null)
        tuesdayRv.adapter = tuesdayAdapter
        tuesdayRv.layoutManager = LinearLayoutManager(this)
        val wednesdayRv: RecyclerView = binding.wednesdayRv
        val wednesdayAdapter = TaskRvAdapter(null)
        wednesdayRv.adapter = wednesdayAdapter
        wednesdayRv.layoutManager = LinearLayoutManager(this)
        val thursdayRv: RecyclerView = binding.thursdayRv
        val thursdayAdapter = TaskRvAdapter(null)
        thursdayRv.adapter = thursdayAdapter
        thursdayRv.layoutManager = LinearLayoutManager(this)
        val fridayRv: RecyclerView = binding.fridayRv
        val fridayAdapter = TaskRvAdapter(null)
        fridayRv.adapter = fridayAdapter
        fridayRv.layoutManager = LinearLayoutManager(this)
        val saturdayRv: RecyclerView = binding.saturdayRv
        val saturdayAdapter = TaskRvAdapter(null)
        saturdayRv.adapter = saturdayAdapter
        saturdayRv.layoutManager = LinearLayoutManager(this)


        // Setting day card names
        clearButtonText.text = "Clear"
        sundayButtonText.text = "Sun"
        mondayButtonText.text = "Mon"
        tuesdayButtonText.text = "Tue"
        wednesdayButtonText.text = "Wed"
        thursdayButtonText.text = "Thu"
        fridayButtonText.text = "Fri"
        saturdayButtonText.text = "Sat"
        sundayButtonText.text = "Sun"

        plannerViewModel.allWeekdays.observe(this, {
            weekdayList = it
        })

        plannerViewModel.allTasks.observe(this, { tasks ->
            taskList = tasks
            taskDayList = mutableListOf()

            for (i in 1..7) {

                taskDayList = sortTasks(weekdayList[i], taskList)

                when (i) {
                    1 -> {
                        sundayAdapter.submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.sundayInner,
                                binding.sundayCardText, sundayRv, binding.sundayNoTasks)
                    }
                    2 -> {
                        mondayAdapter.submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.mondayInner,
                                binding.mondayCardText, mondayRv, binding.mondayNoTasks)
                    }
                    3 -> {
                        tuesdayAdapter.submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.tuesdayInner,
                                binding.tuesdayCardText, tuesdayRv, binding.tuesdayNoTasks)
                    }
                    4 -> {
                        wednesdayAdapter.submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.wednesdayInner,
                                binding.wednesdayCardText, wednesdayRv, binding.wednesdayNoTasks)
                    }
                    5 -> {
                        thursdayAdapter.submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.thursdayInner,
                                binding.thursdayCardText, thursdayRv, binding.thursdayNoTasks)
                    }
                    6 -> {
                        fridayAdapter.submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.fridayInner,
                                binding.fridayCardText, fridayRv, binding.fridayNoTasks)
                    }
                    7 -> {
                        saturdayAdapter.submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.saturdayInner,
                                binding.saturdayCardText, saturdayRv, binding.saturdayNoTasks)
                    }
                }
            }
        })
    }

    private fun toggleVisibility(taskDayList: List<Task>, inner: ConstraintLayout,
                                 cardText: View, rv: RecyclerView, noTask: View) {
        if (taskDayList.count() == 0 ) {
            val newConstraintSet = ConstraintSet()
            newConstraintSet.clone(inner)
            newConstraintSet.connect(noTask.id, ConstraintSet.TOP,
                    cardText.id, ConstraintSet.BOTTOM)
            newConstraintSet.applyTo(inner)

            newConstraintSet.connect(cardText.id, ConstraintSet.BOTTOM,
                    noTask.id, ConstraintSet.TOP)
            newConstraintSet.applyTo(inner)

            rv.visibility = View.GONE
            noTask.visibility = View.VISIBLE

            Log.i("this", "ran zero")
        } else {
            val newConstraintSet = ConstraintSet()
            newConstraintSet.clone(inner)
            newConstraintSet.connect(rv.id, ConstraintSet.TOP,
                    cardText.id, ConstraintSet.BOTTOM)
            newConstraintSet.applyTo(inner)

            newConstraintSet.connect(cardText.id, ConstraintSet.BOTTOM,
                    rv.id, ConstraintSet.TOP)
            newConstraintSet.applyTo(inner)

            rv.visibility = View.VISIBLE
            noTask.visibility = View.GONE

            Log.i("this", "ran else")
        }
    }

    private fun sortTasks(day: Weekday, tasks: List<Task>): MutableList<Task> {
        val newAdapterList = mutableListOf<Task>()

        tasks.forEach {
            if (it.weekdayId == day.id) {
                newAdapterList.add(it)
            }
        }

        return newAdapterList
    }

    private fun startWeekdayActivity(day: Weekday) {
        val intent = Intent(this, WeekdayActivity::class.java)
        intent.putExtra("dayId", day.id)
        this.startActivity(intent)
    }

    private fun clearDb(taskList: List<Task>) {
        val alertDialog: AlertDialog = this.let { outerIt ->
            val builder = AlertDialog.Builder(outerIt)
            builder.apply {
                setPositiveButton("Clear",
                        DialogInterface.OnClickListener { dialog, id ->
                            if (taskList.count() == 0) {
                                Toast.makeText(context, "No tasks to clear", Toast.LENGTH_SHORT).show()
                            } else {
                                plannerViewModel.deleteAllTasks()
                                Toast.makeText(context, "Tasks cleared", Toast.LENGTH_SHORT).show()
                            }
                        })
                setNegativeButton("Cancel",
                        DialogInterface.OnClickListener { dialog, id ->
                            // User cancelled the dialog
                        })
            }
                    .setTitle("Clear tasks?")
                    .setMessage("Are you sure you want to clear the weeks tasks?")

            builder.create()
        }

        alertDialog.show()
    }

    private fun checkDay(dayIn: String, weekdayList: List<Weekday>) {
        weekdayList.forEach {
            if (dayIn == "clear_card" && it.day == "Clear") {
                clearDb(taskList)
            } else {
                val dayInAbr = dayIn.substring(0, 3).toLowerCase(Locale.ROOT)
                val dayOutAbr = it.day.substring(0, 3).toLowerCase(Locale.ROOT)

                if (dayInAbr == dayOutAbr) {
                    startWeekdayActivity(it)
                }
            }
        }
    }

    fun buttonClick(view: View) {
        when (view.id) {
            R.id.clear_card -> checkDay(view.context.resources.getResourceEntryName(R.id.clear_card).toString(), weekdayList)
            R.id.sunday_card -> checkDay(view.context.resources.getResourceEntryName(R.id.sunday_card).toString(), weekdayList)
            R.id.monday_card -> checkDay(view.context.resources.getResourceEntryName(R.id.monday_card).toString(), weekdayList)
            R.id.tuesday_card -> checkDay(view.context.resources.getResourceEntryName(R.id.tuesday_card).toString(), weekdayList)
            R.id.wednesday_card -> checkDay(view.context.resources.getResourceEntryName(R.id.wednesday_card).toString(), weekdayList)
            R.id.thursday_card -> checkDay(view.context.resources.getResourceEntryName(R.id.thursday_card).toString(), weekdayList)
            R.id.friday_card -> checkDay(view.context.resources.getResourceEntryName(R.id.friday_card).toString(), weekdayList)
            R.id.saturday_card -> checkDay(view.context.resources.getResourceEntryName(R.id.saturday_card).toString(), weekdayList)
        }
    }
}

视图模型

class PlannerViewModel(private val repository: DbRepository) : ViewModel() {
    val allWeekdays: LiveData<List<Weekday>> = repository.allWeekdays.asLiveData()
    val allTasks: LiveData<List<Task>> = repository.allTasks.asLiveData()

    fun insertWeekday(weekday: Weekday) = viewModelScope.launch {
        repository.insertWeekday(weekday)
    }

    fun insertTask(task: Task) = viewModelScope.launch {
        repository.insertTask(task)
    }

    fun deleteTask(task: Task) = viewModelScope.launch {
        repository.deleteTask(task)
    }

    fun deleteAllTasks() = viewModelScope.launch {
        repository.deleteAllTasks()
    }
}

class PlannerViewModelFactory(private val repository: DbRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(PlannerViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return PlannerViewModel(repository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}
4

5 回答 5

1

声明为 lateinit 的变量仅意味着您确定当对象被取消引用时它不会为空。在您的情况下,您在为其赋值之前从 weekdayList 对象调用方法。清楚地理解这个概念以及为什么你的代码有效是很重要的。

快乐编码!

于 2021-08-04T04:23:58.940 回答
1

您可以使用“isInitialized”方法来检查“lateinit”变量是否已初始化。

请参阅以下文章以获取此信息-

https://blog.mindorks.com/how-to-check-if-a-lateinit-variable-has-been-initialized

于 2021-08-04T08:57:43.430 回答
1

lateinit是一种让您var在声明时没有初始值的方法。这是一种避免获取永远不会为空的东西并将其设为可空(并且必须永远对其进行空检查)的好方法,这样您就可以暂时将其设置为空作为占位符,什么都不会看到。

你正在做的是向编译器承诺“好的,我不会在构造类时提供一个值,但我保证我会在任何尝试读取它之前将它设置为某个值”。你告诉编译器相信你,你知道你的代码是如何工作的,你可以保证一切都会好起来的。

您的问题是,您似乎无法保证在写入该属性之前不会尝试读取该属性。您的状态可以是“有值”或“没有值”,其余代码可能会遇到任何一种状态。

“无值”状态基本上是null,因此您可能应该将变量设置为可为空,并将其初始化为空。Kotlin 有所有很好的 null 安全性来帮助你的代码处理它,直到你得到一个值。lateinit似乎是适合这项工作的错误工具,即使您检查::isInitialized它只会让您的生活变得更加困难,因为空检查的东西就在那里!

于 2021-08-04T19:11:00.390 回答
0

在评论中得到 cactustictacs 帮助的解决方案。

我将很多列表依赖项移到了一个名为setAdapterList. 这允许两个observes 运行该函数,并且只有两个列表都初始化的那个将运行包含的代码。我保留了变量lateinit,到目前为止它似乎还在工作!

主要活动的主要变化

...

private fun setAdapterLists(adapterList: List<TaskRvAdapter>, rvList: List<RecyclerView>) {
        if (this::weekdayList.isInitialized  && this::taskList.isInitialized) {
            adapterList.forEach {
                taskDayList = mutableListOf()
                val i = adapterList.indexOf(it)
                taskDayList = sortTasks(weekdayList[i + 1], taskList)

                Log.i("rvli", rvList[i].toString())

                when (i) {
                    0 -> {
                        adapterList[i].submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.sundayInner,
                                binding.sundayCardText, rvList[i], binding.sundayNoTasks)
                    }
                    1 -> {
                        adapterList[i].submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.mondayInner,
                                binding.mondayCardText, rvList[i], binding.mondayNoTasks)
                    }
                    2 -> {
                        adapterList[i].submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.tuesdayInner,
                                binding.tuesdayCardText, rvList[i], binding.tuesdayNoTasks)
                    }
                    3 -> {
                        adapterList[i].submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.wednesdayInner,
                                binding.wednesdayCardText, rvList[i], binding.wednesdayNoTasks)
                    }
                    4 -> {
                        adapterList[i].submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.thursdayInner,
                                binding.thursdayCardText, rvList[i], binding.thursdayNoTasks)
                    }
                    5 -> {
                        adapterList[i].submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.fridayInner,
                                binding.fridayCardText, rvList[i], binding.fridayNoTasks)
                    }
                    6 -> {
                        adapterList[i].submitList(taskDayList)
                        toggleVisibility(taskDayList, binding.saturdayInner,
                                binding.saturdayCardText, rvList[i], binding.saturdayNoTasks)
                    }
                }
            }
        }
    }
    

...

于 2021-08-04T04:17:46.247 回答
0

使用惰性属性,有关更多信息,请参阅此文档:

假设weekDayList是您要成功初始化的属性->

private var weekDayList: List<WeekDay> by lazy {
    //return your first value
    listOf<WeekDay>()
}

这是一个关于 LifeCycleAware Lazy 属性的有用链接:blog虽然,它不是必需的。

于 2021-08-05T08:36:20.610 回答