无法创建自定义 ViewModel 的实例
Cannot create an instance of custom ViewModel
我正在使用 dagger2 库。每当我尝试 运行 我的项目都说无法创建视图模型的实例 class.
main activity 我正在尝试创建实例的地方
((MovieApplication) getApplication()).getAppComponent().inject(this);
mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class);
我的工厂class
public class ViewModelFactory implements ViewModelProvider.Factory {
private MoviesDataViewModel mViewModel;
@Inject
public ViewModelFactory(MoviesDataViewModel viewModel) {
this.mViewModel = viewModel;
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
if (modelClass.isAssignableFrom(MoviesDataViewModel.class)) {
return (T) mViewModel;
}
throw new IllegalArgumentException("Unknown class name");
}
我的日志
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.moviedata.viewmodel.MoviesDataViewModel
at android.arch.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:145)
at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(ViewModelProviders.java:143)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:128)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:96)
at com.moviedata.ui.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6321)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access00(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5582)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.InstantiationException: java.lang.Class<com.moviedata.viewmodel.MoviesDataViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
而不是:
mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class);
执行:
mViewModel = ViewModelProviders.of(this, viewModelFactory).get(MoviesDataViewModel.class);
替换:
private MoviesDataViewModel mViewModel;
与:
MoviesDataViewModel mViewModel;
我的解决方案是注入 activity 因为我使用的是 Dagger2
AndroidInjection.inject(this);
如果使用多个 activity
,则需要在所有 activity 中添加以下代码
AndroidInjection.inject(this);
从您的 ViewModel 扩展 AndroidViewModel class。
public class YourViewModel extends AndroidViewModel {
public YourViewModel(Application application) {
super(application);
//Todo: ...
}
}
我是一名熟练的 Android 开发人员,我已经使用 ViewModel 100 多次,没有任何问题。今天我遇到了这个问题。花了几个小时浏览了各种 SO 帖子。没解决。
然后我看到我的ViewModel所在的包名包含new。像这样:
com.myapp.myfeature.new.feature
我将 new 更改为 neww 以进行如下测试:com.myapp.myfeature.neww.feature
成功了!我希望有人觉得它有用。
我正在使用 dagger2 库。每当我尝试 运行 我的项目都说无法创建视图模型的实例 class.
main activity 我正在尝试创建实例的地方
((MovieApplication) getApplication()).getAppComponent().inject(this);
mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class);
我的工厂class
public class ViewModelFactory implements ViewModelProvider.Factory {
private MoviesDataViewModel mViewModel;
@Inject
public ViewModelFactory(MoviesDataViewModel viewModel) {
this.mViewModel = viewModel;
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
if (modelClass.isAssignableFrom(MoviesDataViewModel.class)) {
return (T) mViewModel;
}
throw new IllegalArgumentException("Unknown class name");
}
我的日志
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.moviedata.viewmodel.MoviesDataViewModel
at android.arch.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:145)
at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(ViewModelProviders.java:143)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:128)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:96)
at com.moviedata.ui.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6321)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access00(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5582)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.InstantiationException: java.lang.Class<com.moviedata.viewmodel.MoviesDataViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
而不是:
mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class);
执行:
mViewModel = ViewModelProviders.of(this, viewModelFactory).get(MoviesDataViewModel.class);
替换:
private MoviesDataViewModel mViewModel;
与:
MoviesDataViewModel mViewModel;
我的解决方案是注入 activity 因为我使用的是 Dagger2
AndroidInjection.inject(this);
如果使用多个 activity
,则需要在所有 activity 中添加以下代码AndroidInjection.inject(this);
从您的 ViewModel 扩展 AndroidViewModel class。
public class YourViewModel extends AndroidViewModel {
public YourViewModel(Application application) {
super(application);
//Todo: ...
}
}
我是一名熟练的 Android 开发人员,我已经使用 ViewModel 100 多次,没有任何问题。今天我遇到了这个问题。花了几个小时浏览了各种 SO 帖子。没解决。
然后我看到我的ViewModel所在的包名包含new。像这样:
com.myapp.myfeature.new.feature
我将 new 更改为 neww 以进行如下测试:com.myapp.myfeature.neww.feature
成功了!我希望有人觉得它有用。