简体   繁体   English

ViewModelProviders 在 1.1.0 中已弃用

[英]ViewModelProviders is deprecated in 1.1.0

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,他们展示了以下关于如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1时没有这样的 class ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此 class 在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory时,找不到等效of方法来获取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.因此方法的名称create ,我每次调用它时都会得到一个 ViewModel 的新实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?有什么想法可以替换上面不推荐使用的代码吗?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

There is nothing about the get method that is deprecated . deprecated get方法。 If you follow the Android Developer instructions then you should get the VM like this. 如果您遵循Android Developer说明,那么您应该像这样获得VM

public class MyActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
    // Create a ViewModel the first time the system calls an activity's onCreate() method.
    // Re-created activities receive the same MyViewModel instance created by the first activity.

    MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
    model.getUsers().observe(this, users -> {
        // update UI
    });
  }
}

Here is the instructions on how to use ViewModels Here is the instructions on how to set up your project with the correct dependencies 以下是有关如何使用ViewModels的说明以下是有关如何使用正确的依赖项设置项目的说明

在此输入图像描述

在此输入图像描述

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Simply update from只需从更新

ViewModelProviders.of(this, provider).get(MainActivityViewModel::class.java) ViewModelProviders.of(this, provider).get(MainActivityViewModel::class.java)

to

ViewModelProvider(this, provider).get(MainActivityViewModel::class.java) ViewModelProvider(this, provider).get(MainActivityViewModel::class.java)

You can use something like this:你可以使用这样的东西:

private  val sharedViewModel: SharedViewModel by viewModels(ownerProducer = { requireActivity() })


ownerProducer = { requireActivity() } // This is only required if you want to share same view model among different fragments.

I am using Library of Lifecycle 2.3.1 and add below dependency in build.gradle (Module-level)我正在使用生命周期库 2.3.1 并在build.gradle (模块级)中添加以下依赖

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.2.0'
implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.fragment:fragment-ktx:1.3.6'

Declare viewModels() in your Kotlin code as per below:在您的Kotlin代码中声明viewModels()如下:

import androidx.fragment.app.viewModels

private val cartViewModel: CartViewModel by viewModels()

Update: July 20 2022更新:2022 年 7 月 20 日

private lateinit var binding: FragmentHomeBinding private lateinit var homeMvvM : HomeViewModel私有lateinit var绑定:FragmentHomeBinding私有lateinit var homeMvvM:HomeViewModel

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    homeMvvM = ViewModelProvider(this)[HomeViewModel::class.java]

this works for me.这对我有用。

//Activity ktx
implementation 'androidx.activity:activity-ktx:1.5.1'

//Fragment ktx
implementation 'androidx.fragment:fragment-ktx:1.5.1'

Simplest way of creating view models:创建视图模型的最简单方法:

class MyViewModel() : ViewModel() {

}

class SomeActivity:AppCompatActivity(){
  private val viewModel: MyViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_some)
}
}

View Model With Constructor Arguments:使用构造函数 Arguments 查看 Model:

class MyViewModel(list:List<String>) : ViewModel() {


 class Factory(list:List<String>) : ViewModelProvider.Factory {
        @Suppress("unchecked_cast")
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return MyViewModel(list) as T
        }
    }
}
class SomeActivity:AppCompatActivity(){

  private val viewModel: MyViewModel by viewModels(){
MyViewModel.Factory(listOf<String>("a"))}

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

Try adding this code in your build.gradle module level尝试在您的 build.gradle 模块级别添加此代码

 android {
 kotlinOptions {
    freeCompilerArgs += [
        '-Xjvm-default=enable'
    ]
  }   
}

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel :查看ViewModelGoogle 文档,它们显示了有关如何获取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 .当使用最新的依赖android.arch.lifecycle:extensions:1.1.1 ,没有这样的类ViewModelProviders

Going to the documentation for ViewModelProviders , I saw a comment saying:转到ViewModelProviders文档,我看到一条评论说:

This class was deprecated in API level 1.1.0.此类在 API 级别 1.1.0 中已弃用。 Use ViewModelProvider.AndroidViewModelFactory使用 ViewModelProvider.AndroidViewModelFactory

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory , cannot find an equivalent of method to get the instance of the ViewModel .问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory ,找不到等效of方法来获取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.因此,方法create的名称,每次我调用它时都会得到一个新的 ViewModel 实例,这不是我所追求的。

Any ideas what is the replacement of deprecated code above?任何想法替换上面已弃用的代码是什么?

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

相关问题 已弃用的 ViewModelProviders 的替代方案? - Alternative for deprecated ViewModelProviders? 不推荐使用“ViewModelProviders”。 Java解决方案? - 'ViewModelProviders' is deprecated. Java solution? 为什么 ViewModelProviders 已被弃用? - Why has ViewModelProviders been deprecated? 如何在 JAVA 中弃用 ViewModelProviders 后实现 ViewModelProvider - How to implement ViewModelProvider after ViewModelProviders deprecated in JAVA 由于不推荐使用 ViewModelProviders.of(),我应该如何创建 ViewModel 的 object? - As ViewModelProviders.of() is deprecated, how should I create object of ViewModel? 当 ViewModelProviders.of() 被弃用时,如何在 Activity 和 Service 之间共享相同的 ViewModel? - How to share the same ViewModel between Activity and Service when ViewModelProviders.of() is deprecated? 'ViewModelProviders' 已弃用。 升级生命周期扩展后:2.1.0 到 2.2.0-alpha05 - 'ViewModelProviders' is deprecated. After upgrading lifecycle-extensions:2.1.0 to 2.2.0-alpha05 Android Studio(AS)1.1.0(和Eclipse Luna)“设置活动”中不推荐使用的方法 - Deprecated methods in Android Studio (AS) 1.1.0 (and Eclipse Luna) “Settings Activity” 无法解析符号 ViewModelProviders - Cannot resolve symbol ViewModelProviders 如何在 Kotlin 中使用 ViewModelProviders - How to use ViewModelProviders in Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM