我正在学习使用Kotlin开发Android应用程序的UDACITY免费课程,实际上我正在学习该课程的Viewmodel/MVM部分,即实现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)
这似乎是正确的方法,但我仍然在Fragment类中的变量word(gameViewModel.word(和score(gameVViewModel.
score(上留下了一些未解决的引用,无法编译。我不知道我是否正确地声明了Viewmodel对象,或者我是否遗漏了其他内容。。。
我的ViewModel类函数,即gameViewModel.onCorrect((和gameViewModel.onSkip((,没有这个问题,Unsolved reference。它们似乎在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()
}
}
我哪里搞砸了?
您试图访问的变量不属于同一范围:
var word = ""
var score = 0
private lateinit var wordList: MutableList<String>
class GameViewModel: ViewModel() {
...
变量是在ViewModel之外声明的,请将它们添加到类GameViewModel
中,使其成为实例变量:
class GameViewModel: ViewModel() {
var word = ""
var score = 0
...
}