久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ViewModelProviders 在 1.1.0 中已棄用

ViewModelProviders is deprecated in 1.1.0(ViewModelProviders 在 1.1.0 中已棄用)
本文介紹了ViewModelProviders 在 1.1.0 中已棄用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

查看 ViewModel 的 Google 文檔,他們展示了以下關(guān)于如何獲取 ViewModel 的示例代碼:

val model = ViewModelProviders.of(this).get(MyViewModel::class.java)

當(dāng)使用最新的依賴android.arch.lifecycle:extensions:1.1.1時沒有ViewModelProviders這個類.

轉(zhuǎn)到 ViewModelProviders,我看到一個評論說:

<塊引用>

此類在 API 級別 1.1.0 中已棄用.使用 ViewModelProvider.AndroidViewModelFactory

問題是,當(dāng)嘗試使用ViewModelProvider.AndroidViewModelFactory 時,找不到等效的of 方法來獲取ViewModel 的實例.

我嘗試做什么:

ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(PlayerViewHolder::class.java)

因此方法名為 create,我每次調(diào)用它時都會得到一個新的 ViewModel 實例,這不是我想要的.

有什么想法可以替換上面不推薦使用的代碼嗎?

解決方案

UPDATE 2020-06-16:目前 ViewModelProviders 已棄用,不應(yīng)再使用.這個問題和答案來自 2018 年底,當(dāng)時情況并非如此.此問題和答案也適用于 ViewModelProviders 的舊架構(gòu)組件版本,而不是 AndroidX 版本.

<小時><塊引用>

當(dāng)使用最新的依賴android.arch.lifecycle:extensions:1.1.1時沒有ViewModelProviders這個類.

是的,有.為了證明這一點:

  • 在 Android Studio 3.2.1 中創(chuàng)建一個新項目(使用 Kotlin,minSdkVersion 21,空活動"模板)

  • app模塊的依賴中添加android.arch.lifecycle:extensions:1.1.1

這會給你一個 app/build.gradle 像:

應(yīng)用插件:'com.android.application'應(yīng)用插件:'kotlin-android'應(yīng)用插件:'kotlin-android-extensions'安卓 {compileSdkVersion 28默認配置 {應(yīng)用程序IDcom.commonsware.myandroidarch"minSdkVersion 21targetSdkVersion 28版本代碼 1版本名稱1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}構(gòu)建類型 {發(fā)布 {縮小啟用假proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}}依賴{實現(xiàn)文件樹(目錄:'libs',包括:['*.jar'])實施org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"實施 'com.android.support:appcompat-v7:28.0.0'實施 'com.android.support.constraint:constraint-layout:1.1.3'實現(xiàn) 'android.arch.lifecycle:extensions:1.1.1'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}

然后您將看到該庫與該類一起顯示在外部庫"中:

您將能夠引用該類:

包 com.commonsware.myandroidarch導(dǎo)入 android.arch.lifecycle.ViewModelProviders導(dǎo)入 android.support.v7.app.AppCompatActivity導(dǎo)入 android.os.Bundle類 MainActivity : AppCompatActivity() {覆蓋 fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val provider = ViewModelProviders.of(this)}}

<塊引用>

查看 ViewModelProviders 的文檔時,我看到一條評論說:此類在 API 級別 1.1.0 中已棄用.使用 ViewModelProvider.AndroidViewModelFactory

該注釋位于 ViewModelProviders.DefaultFactory 類條目下方,指的是該類,而不是 ViewModelProviders:

<塊引用>

有什么想法可以替換上面不推薦使用的代碼嗎?

使用 ViewModelProviders.

Looking at the Google docs for ViewModel, they show the below sample code on how to get a ViewModel:

val model = ViewModelProviders.of(this).get(MyViewModel::class.java)

When using the latest dependency android.arch.lifecycle:extensions:1.1.1 there is no such class ViewModelProviders.

Going to the documentation for ViewModelProviders, I saw a comment saying:

This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory, cannot find an equivalent of method to get the instance of the ViewModel.

What i tried doing:

ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(PlayerViewHolder::class.java)

Hence the name of the method create, I get a new instance of the ViewModel every-time I call it, which is not what I am after.

Any ideas what is the replacement of deprecated code above?

解決方案

UPDATE 2020-06-16: Presently ViewModelProviders is deprecated and should no longer be used. This question and answer were from late 2018, when that was not the case. This question and answer are also for the older Architecture Components edition of ViewModelProviders, not the AndroidX edition.


When using the latest dependency android.arch.lifecycle:extensions:1.1.1 there is no such class ViewModelProviders.

Yes, there is. To demonstrate this:

  • Create a new project in Android Studio 3.2.1 (with Kotlin, minSdkVersion 21, "empty activity" template)

  • Add android.arch.lifecycle:extensions:1.1.1 to the dependencies of the app module

This will give you an app/build.gradle like:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.commonsware.myandroidarch"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

You will then see that library show up in "External Libraries" with that class:

And you will be able to reference that class:

package com.commonsware.myandroidarch

import android.arch.lifecycle.ViewModelProviders
import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val provider = ViewModelProviders.of(this)
  }
}

Going to the documentation for ViewModelProviders, I saw a comment saying: This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory

That comment is underneath the ViewModelProviders.DefaultFactory class entry and refers to that class, not ViewModelProviders:

Any ideas what is the replacement of deprecated code above?

Use ViewModelProviders.

這篇關(guān)于ViewModelProviders 在 1.1.0 中已棄用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現(xiàn) IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當(dāng)前風(fēng)味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復(fù)“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風(fēng)味庫的多風(fēng)味應(yīng)用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 日本不卡一区二区 | 亚洲精品久久国产高清情趣图文 | 日韩二区三区 | 国产精品一区二区三 | 久久久久久久久久久高潮一区二区 | 成人欧美一区二区三区黑人孕妇 | 亚洲一区二区三区视频免费观看 | 精品国产乱码久久久久久1区2区 | 一级网站| 国产日韩av一区二区 | 在线免费观看黄色 | 日日网| 欧美韩一区二区三区 | 免费看黄视频网站 | 日韩精品1区2区 | 99国产欧美| 国产日韩免费视频 | 成人福利视频网站 | av在线播放一区二区 | 欧美中文字幕 | 色网站视频 | 亚洲精品欧美精品 | 三级高清 | 91精品国产综合久久久久久 | 国产日韩欧美精品一区二区 | 日韩欧美在线观看视频网站 | 久久精品久久精品久久精品 | 欧美日韩在线观看一区二区三区 | 日韩视频免费看 | 欧洲精品久久久久毛片完整版 | av中文天堂| 九九激情视频 | 午夜精品一区 | 国产一区精品在线 | 91精品国产一二三 | 国产亚洲精品精品国产亚洲综合 | 少妇一级淫片免费播放 | av网站免费在线观看 | 日韩www | 99久久久国产精品 | 国产免费xxx |