未使用 class 实例获取变量名:Kotlin
Not getting a variable name using class instance: Kotlin
我正在关注有关 LiveData 的旧教程,ViewModel.These 是链接。
- [https://www.youtube.com/watch?v=d7UxPYxgBoA][1]
- [https://resocoder.com/2018/09/07/mvvm-on-android-crash-course-kotlin-android-architecture-components/]
我在 InjectUtil.kt class 中遇到错误,同时启动 FakeDatabase class 并获取它的变量。我正在学习教程,但不知道为什么会这样。
//InjectUtil Object
package dk.humma.livedata_viewmodel.utilities
import dk.humma.livedata_viewmodel.data.FakeDataBase
import dk.humma.livedata_viewmodel.data.QuotesRepository
import dk.humma.livedata_viewmodel.ui.quotes.QuotesViewModelFactory
// Finally a singleton which doesn't need anything passed to the constructor
object InjectorUtils {
// This will be called from QuotesActivity
fun provideQuotesViewModelFactory(): QuotesViewModelFactory {
// The whole dependency tree is constructed right here, in one place
val quoteRepository = QuotesRepository.getInstance(FakeDataBase.getInstance().quoteDao)
//Getting error while trying to get quoteDao variable
// Not accessing quoteDao
return QuotesViewModelFactory(quoteRepository)
}
}
//FakeDataBase class
package dk.humma.livedata_viewmodel.data
class FakeDataBase private constructor(){
var quoteDao = DataTable()
private set
companion object {
@Volatile private var instance : FakeDataBase? = null
fun getInstance() {
instance?: synchronized(this){
instance?: FakeDataBase().also { instance = it }
}
}
}
}
有人知道吗?非常感谢。
您的 getInstance
函数没有 returning 任何东西 - 如果您查看该函数的文档弹出窗口,它将有一个 return 类型的 Unit
. Unit
没有 quoteDao
属性,这就是 IDE 抱怨的原因。如果你遇到这样的错误,请检查你试图访问它的变量的类型
您可以通过添加 return
(并在它抱怨时添加类型)
来修复它
fun getInstance() : FakeDataBase {
return instance?: synchronized(this){
instance?: FakeDataBase().also { instance = it }
}
}
或作为表达式(=
而不是功能块,后者需要 return
)
fun getInstance() = instance?: synchronized(this) {
instance?: FakeDataBase().also { instance = it }
}
我正在关注有关 LiveData 的旧教程,ViewModel.These 是链接。
- [https://www.youtube.com/watch?v=d7UxPYxgBoA][1]
- [https://resocoder.com/2018/09/07/mvvm-on-android-crash-course-kotlin-android-architecture-components/]
我在 InjectUtil.kt class 中遇到错误,同时启动 FakeDatabase class 并获取它的变量。我正在学习教程,但不知道为什么会这样。
//InjectUtil Object
package dk.humma.livedata_viewmodel.utilities
import dk.humma.livedata_viewmodel.data.FakeDataBase
import dk.humma.livedata_viewmodel.data.QuotesRepository
import dk.humma.livedata_viewmodel.ui.quotes.QuotesViewModelFactory
// Finally a singleton which doesn't need anything passed to the constructor
object InjectorUtils {
// This will be called from QuotesActivity
fun provideQuotesViewModelFactory(): QuotesViewModelFactory {
// The whole dependency tree is constructed right here, in one place
val quoteRepository = QuotesRepository.getInstance(FakeDataBase.getInstance().quoteDao)
//Getting error while trying to get quoteDao variable
// Not accessing quoteDao
return QuotesViewModelFactory(quoteRepository)
}
}
//FakeDataBase class
package dk.humma.livedata_viewmodel.data
class FakeDataBase private constructor(){
var quoteDao = DataTable()
private set
companion object {
@Volatile private var instance : FakeDataBase? = null
fun getInstance() {
instance?: synchronized(this){
instance?: FakeDataBase().also { instance = it }
}
}
}
}
有人知道吗?非常感谢。
您的 getInstance
函数没有 returning 任何东西 - 如果您查看该函数的文档弹出窗口,它将有一个 return 类型的 Unit
. Unit
没有 quoteDao
属性,这就是 IDE 抱怨的原因。如果你遇到这样的错误,请检查你试图访问它的变量的类型
您可以通过添加 return
(并在它抱怨时添加类型)
fun getInstance() : FakeDataBase {
return instance?: synchronized(this){
instance?: FakeDataBase().also { instance = it }
}
}
或作为表达式(=
而不是功能块,后者需要 return
)
fun getInstance() = instance?: synchronized(this) {
instance?: FakeDataBase().also { instance = it }
}