无法将类型强制转换为“ ListViewModel”,因为“ binding.listViewModel”是一个可变属性,可能此时已更改

时间:2019-11-03 19:17:04

标签: android kotlin

我正在用kotlin为Android制作一个小型任务程序。我将主要活动分为几个片段,然后为每个片段创建一个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,但是我却得到了一个例外,该问题的标题中提到了此例外。谢谢您的帮助!

1 个答案:

答案 0 :(得分:3)

代替使用

binding.listViewModel.tasks.observe(this, Observer {

只需使用您已经拥有的listViewModel变量:

listViewModel.tasks.observe(this, Observer {

之所以有效,是因为您的listViewModelval-即一个不可变的,永不为null的变量。这与binding.listViewModel不同,后者是一个可变的,可为空的变量-尽管您知道自调用binding.listViewModel = listViewModel以来它不为null,但是编译器不知道是否还有其他线程将其设置为null。