我正在学习有关使用Kotlin开发Android应用程序的UDACITY免费课程,而我实际上是在本课程的Viewmodel / MVVM部分,即实现Viewmodel类以更好地分离关注点。所以无论如何,我现在就封锁了。我正在进行的练习是关于创建Viewmodel类并将变量和函数从Fragment类传递到这个新创建的类的。我逐步按照教程进行操作,检查所提供的git diff的正确答案,但仍然发现自己被未解决的参考错误所阻止。
更改代码之前,我必须更新Gradle模块文件以使用ViewModel
//ViewModel
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
然后我必须在Fragment类中声明Viewmodel对象
gameViewModel = ViewModelProviders.of(this).get(GameViewModel::class.java)
不推荐使用ViewModelProviders,以前的课程,在搜索后,我不得不将其更改为
gameViewModel = ViewModelProvider(this).get(GameViewModel::class.java)
这似乎是正确的方法,但是我仍然对变量word(gameViewModel。word)和得分(gameViewModel。score] >)在Fragment类中,无法编译。我不知道我是否正确声明了Viewmodel对象,或者是否缺少其他内容...
我没有ViewModel类函数(即gameViewModel.onCorrect()和gameViewModel.onSkip()的问题,未解决的参考)。它们似乎在Fragment类中已正确声明和调用,这使我对变量,单词和分数产生了疑问...
我的片段类
package com.example.android.guesstheword.screens.game import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import androidx.navigation.fragment.NavHostFragment.findNavController import com.example.android.guesstheword.R import com.example.android.guesstheword.databinding.GameFragmentBinding import timber.log.Timber class GameFragment : Fragment() { private lateinit var binding: GameFragmentBinding private lateinit var gameViewModel: GameViewModel override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { // Inflate view and obtain an instance of the binding class binding = DataBindingUtil.inflate( inflater, R.layout.game_fragment, container, false ) Timber.i("onCreateView GameFragment called") gameViewModel = ViewModelProvider(this).get(GameViewModel::class.java) Timber.i("ViewModelProvider is called") binding.correctButton.setOnClickListener { gameViewModel.onCorrect() updateScoreText() updateWordText() } binding.skipButton.setOnClickListener { gameViewModel.onSkip() updateScoreText() updateWordText() } updateScoreText() updateWordText() return binding.root } /** * Called when the game is finished */ fun gameFinished() { val action = GameFragmentDirections.actionGameToScore(gameViewModel.score) findNavController(this).navigate(action) } /** Methods for updating the UI **/ private fun updateWordText() { binding.wordText.text = gameViewModel.word } private fun updateScoreText() { binding.scoreText.text = gameViewModel.score.toString() } override fun onDestroyView() { super.onDestroyView() Timber.i("onDestroyView GameFragment called") } }
我的ViewModel类
package com.example.android.guesstheword.screens.game import androidx.lifecycle.ViewModel import timber.log.Timber var word = "" var score = 0 private lateinit var wordList: MutableList<String> class GameViewModel: ViewModel() { init { Timber.i("GameViewModel is created") resetList() nextWord() } override fun onCleared() { super.onCleared() Timber.i("GameViewModel is cleared") } /** * Resets the list of words and randomizes the order */ fun resetList() { wordList = mutableListOf( "queen", "hospital", "basketball", "cat", "change", "snail", "soup", "calendar", "sad", "desk", "guitar", "home", "railway", "zebra", "jelly", "car", "crow", "trade", "bag", "roll", "bubble" ) wordList.shuffle() } /** * Moves to the next word in the list */ private fun nextWord() { //Select and remove a word from the list if (wordList.isEmpty()) { //gameFinished() } else { word = wordList.removeAt(0) } } /** Methods for buttons presses **/ fun onSkip() { score-- nextWord() } fun onCorrect() { score++ nextWord() } }
我在哪里弄糟的?
[我正在学习有关用Kotlin开发Android应用程序的UDACITY免费课程,而我实际上是在本课程的Viewmodel / MVVM部分,即实现Viewmodel类以更好地分离...
您尝试访问的变量不在同一范围内: