[英]error: no suitable method found for method ViewModelProviders.of(Fragment,Factory)
Trying to provde ViewModel
via ViewModelProviers.of()
to my activity. 试图provde
ViewModel
通过ViewModelProviers.of()
到我的活动。
My activity extends AppCompatActivity
but I still see this error: 我的活动扩展了
AppCompatActivity
但仍然看到此错误:
error: no suitable method found for of(StartActivity,StartViewModelFactory) method ViewModelProviders.of(Fragment,Factory) is not applicable (argument mismatch; StartActivity cannot be converted to Fragment) method ViewModelProviders.of(FragmentActivity,Factory) is not applicable (argument mismatch; StartActivity cannot be converted to FragmentActivity)
错误:找不到适用于of(StartActivity,StartViewModelFactory)方法ViewModelProviders.of(Fragment,Factory)的合适方法(参数不匹配; StartActivity无法转换为Fragment)方法ViewModelProviders.of(FragmentActivity,Factory)不适用(参数不匹配; StartActivity无法转换为FragmentActivity)
Dependency Injection is provided with Dagger 2. Below is my Activity class StartActivity: Dagger 2提供了依赖注入。下面是我的Activity类StartActivity:
public class StartActivity extends AppCompatActivity {
@Inject
StartViewModelFactory startViewModelFactory;
StartModelView startModelView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
startModelView = ViewModelProviders.of(this, startViewModelFactory).
get(StartViewModel.class);
}
If I understand correctly you should be able to send in an activity in ViewModelProviders.of()
method if they extend AppCompatActivity or Fragment. 如果我正确理解,如果它们扩展了AppCompatActivity或Fragment,则应该能够在
ViewModelProviders.of()
方法中发送活动。 But I get this error. 但是我得到这个错误。 Any help would be appreciated.
任何帮助,将不胜感激。
you should be using 你应该使用
.get(StartModelView.class);
and not 并不是
.get(StartViewModelFactory.class);
the factory provided to of()
is used by android to generate an instance of your ViewModel
subclass android使用提供给
of()
的工厂来生成ViewModel
子类的实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.