我手动将我的应用程序从dagger2迁移到了刀柄。我没有遇到任何编译或构建错误。但当我尝试运行我的应用程序时,它会在启动屏幕上崩溃:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pfe.usdb.dictili, PID: 7815
java.lang.RuntimeException: Unable to start activity ComponentInfo{pfe.usdb.dictili/pfe.usdb.dictili.view.auth.SplashScreen}: java.lang.RuntimeException: Cannot create an instance of class pfe.usdb.dictili.viewmodel.auth.AuthVModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.RuntimeException: Cannot create an instance of class pfe.usdb.dictili.viewmodel.auth.AuthVModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
at androidx.hilt.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:74)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
at pfe.usdb.dictili.view.auth.SplashScreen.getViewModel(Unknown Source:2)
at pfe.usdb.dictili.view.auth.SplashScreen.onCreate(SplashScreen.kt:46)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.InstantiationException: java.lang.Class<pfe.usdb.dictili.viewmodel.auth.AuthVModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
at androidx.hilt.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:74)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
at pfe.usdb.dictili.view.auth.SplashScreen.getViewModel(Unknown Source:2)
at pfe.usdb.dictili.view.auth.SplashScreen.onCreate(SplashScreen.kt:46)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
它说它不能创建那个viewmodel的实例,因为它没有零参数构造函数,但是:1-我的viewmodel类是用hilt注入的(BaseViewModel是抽象的):
class AuthVModel @ViewModelInject constructor(
private val sharedPreferences: PreferencesRepository,
private val authRepository: AuthRepository
) : BaseViewModel()
2-我的活动用Android Entry Point注释(Base活动是抽象的):
@AndroidEntryPoint
class SplashScreen : BaseActivity() {
private val viewModel: AuthVModel by viewModels()
3-视图模型所依赖的存储库已正确注入,Android Studio 4.1.1在左侧显示注入图标:
class AuthRepository @Inject constructor(
@NonAuthenticatedAuthenticationService private val unauthorizedService: AuthService,
@AuthenticatedAuthenticationService private val authorizedService: AuthService
)
class PreferencesRepository @Inject constructor(
private val sharedPreferences: SharedPreferences
)
我不知道这里发生了什么,这不是我第一次用这个工作,因为我最近建立了一些这样的应用程序(一些代码是100%复制自这些项目)。
您是否在模块的gradle.build
文件中使用了jetpad组件所需的依赖项(在您的案例中为ViewModel)?
即:
kapt "androidx.hilt:hilt-compiler:$hiltJetpackVer"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltJetpackVer"
请注意,版本与用于主刀柄依赖项的版本不同。
我通过本文档了解LiveData和ViewModel。在文档中,ViewModel类具有构造函数, 但是,当我运行代码时,会出现异常: 原因:java.lang.运行时异常:无法创建类的实例UserViewModel原因:java.lang.实例异常:java.lang.类没有零参数构造函数
我正在尝试使用recyclerview和room库创建一个简单的ToDoList应用程序。在使用room和mvvm架构方面,我正在遵循android开发者代码实验室,我似乎遇到了困难。我已经设置了应用程序的每一层,但在尝试使用ViewModelProvider实例化ViewModel时出现了一个错误。下面是我的ViewModel类中的代码。 } 下面是我试图初始化ViewModel的main片段中
我正在使用并试图将我的导入我的类。 当使用ViewModel查看Google的hilt文档时,我们可以看到他们能够将注入到所述
我是新的Android和Java,并试图使基于位置的应用程序。 编辑 我做了一个非常非常简单的测试代码,得到了同样的错误。这是java: 我也犯了同样的错误。以下是我的应用程序级构建中的依赖项。格雷德尔: 原帖 我试图使用ViewModel和LiveData来更新用户位置,因为我知道这是生命周期感知的最佳方式。我有一个默认的地图活动... 一个扩展LiveData以存储用户位置的类。。。 以及一个
注:在标记为副本之前,请仔细阅读。我已经尝试了所有现有的答案。 我在用匕首2。 视图模型实例已在活动中成功创建,但当我从同一活动中打开BottomSheet对话框时,无法创建实例。 日志: 用户界面。搜索viewmodel。SearchViewModel
我得到了MyViewModel,它的构造函数是空的 我想提供这样的新模型,但有一个问题: 我确实实现了ViewModelProvider.Factory 但我不知道该怎么办,把没有指标的工厂退回去。如何实现? 谢谢