简体   繁体   English

为什么在尝试实现 ViewModel 时出现此错误?

[英]Why am i getting this error when trying to Implement ViewModel?

I am trying to learn about ViewModels so i am following the instructions from the Android Developer docs, but unfortunately I am receiving this message i attached a screenshot.我正在尝试了解 ViewModels,所以我按照 Android 开发人员文档中的说明进行操作,但不幸的是,我收到了这条消息,我附上了截图。 All I have really done so far was added necessary implementations stated on the website and followed what was stated on the doc to implement it.到目前为止,我所做的只是添加了网站上说明的必要实现,并按照文档中的说明来实现它。

  def lifecycle_version = "2.6.0-alpha02"
    def arch_version = "2.1.0"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    // Lifecycles only (without ViewModel or LiveData)
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

    // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

    //Was not recognizing ViewModelProvider so i added this to fix it..
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation "android.arch.lifecycle:viewmodel:1.1.1"

在此处输入图像描述

Add second parameter ViewModelFactory in ViewModelProvider在 ViewModelProvider 中添加第二个参数 ViewModelFactory

val factory = YourViewModelFactory(YourRepository())     
viewModel = ViewModelProvider(this, factory).get(yourViewModelclass::class.java)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试与我的服务器通信时出现此错误? - Why am i getting this error when trying to communicate with my server? 为什么当我尝试点击 RabbitMQ 中的 URL 时出现通道错误? - Why I am getting Channel error when I am trying to hit the URL in RabbitMQ? 尝试在 Android 工作室中实现 tensorflow lite model 时,我收到不兼容的数据类型错误 - When trying to implement tensorflow lite model in Android studio I am getting an incompatible data types error 尝试 setOnClickListener 时出现错误 - I am getting error when trying to setOnClickListener 尝试在Java Spring中获取JSON时为什么会出现406错误? - Why am I getting a 406 error when trying to get JSON in Java Spring? 为什么在尝试打印单独的星星行时,索引 0 超出长度 0 的范围会出现错误? - Why am I getting an error for index 0 out of bounds for length 0 when trying to print separate rows of stars? 为什么在类型转换器的帮助下尝试在 Room 数据库中保存列表时出现错误? - Why am I getting an error when trying to save a list in Room database with the help of a type converter? 为什么在尝试查看字符串是否以特定字符结尾时会出现类型不兼容的错误? - Why am I getting an incompatible types error when trying to see if a String ends with a specific character? 为什么我在尝试使用 LWJGL 库加载声音时收到错误“NoClassDefFoundError: sun/misc/Unsafe”? - Why am I getting the error “NoClassDefFoundError: sun/misc/Unsafe” when trying to load sounds using the LWJGL library? 为什么在尝试使用局部变量时出现“找不到符号”错误? - Why am I getting a “cannot find symbol” error when trying to use a local variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM