我正在用Kotlin为Android做一个小任务工。我将主要 Activity 分为几个片段,然后为每个片段创建一个ViewModel。但是现在这里出现了智能投放的问题。
这是一些代码:
class ListFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val binding: FragmentListBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_list, container, false)
val application = requireNotNull(this.activity).application
val dataSource = TaskDatabase.getInstance(application).taskDatabaseDao
val viewModelFactory = ListViewModelFactory(dataSource, application)
val listViewModel =
ViewModelProviders.of(this, viewModelFactory).get(ListViewModel::class.java)
binding.listViewModel = listViewModel
binding.setLifecycleOwner(this)
binding.addButton.setOnClickListener {
findNavController().navigate(R.id.action_listFragment_to_createTaskFragment)
}
binding.listViewModel.tasks.observe(this, Observer {
it?.forEach {
Log.i("ListFragment", "tasks exist")
val radioButton = RadioButton(context)
radioButton.text = it.title
radioButton.visibility = View.VISIBLE
radioButton.setOnClickListener {
Toast.makeText(context, "Working", Toast.LENGTH_SHORT).show()
}
}
})
return binding.root
}
}
class ListViewModel(
val database: TaskDao,
application: Application) : AndroidViewModel(application) {
private val viewModelJob = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
private val _tasks = MutableLiveData<List<Task>>()
val tasks: LiveData<List<Task>>
get() = _tasks
init {
initializeTasks()
}
private fun initializeTasks() {
uiScope.launch {
_tasks.value = getTasksFromDatabase()?.value
Log.i("ListViewModel", "something")
}
}
private suspend fun getTasksFromDatabase(): LiveData<List<Task>>? {
return withContext(Dispatchers.IO) {
database.getAllTasks()
}
}
override fun onCleared() {
super.onCleared()
viewModelJob.cancel()
}
}
我本来希望在观察者方法的帮助下在主屏幕上看到一些创建的radioButtons,但是我得到了一个异常(exception),该问题的标题中提到了此异常(exception)。预先感谢您的帮助!
最佳答案
而不是使用
binding.listViewModel.tasks.observe(this, Observer {
只需使用您已经拥有的
listViewModel
变量:listViewModel.tasks.observe(this, Observer {
这是有效的,因为您的
listViewModel
是val
-即一个不可变的,从不为null的变量。这不同于binding.listViewModel
,后者是一个可变的,可为null的变量-尽管您知道它不是null,因为调用了binding.listViewModel = listViewModel
,但编译器不知道是否还有其他线程将其设置为null。