java.lang.RuntimeException:无法创建类MovieViewModel的实例&存储库尚未初始化

时间:2019-02-28 21:36:17

标签: android viewmodel dagger-2

当我注入viewModel和存储库时,它给了我这个错误,但我认为我做对了。

登录错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.abousalem.movies/com.apps.abousalem.movies.ui.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property repository has not been initialized
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2457)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2519)
    at android.app.ActivityThread.access$1200(ActivityThread.java:165)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5669)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 Caused by: kotlin.UninitializedPropertyAccessException: lateinit property repository has not been initialized
    at com.apps.abousalem.movies.ui.base.BaseViewModel.getRepository(BaseViewModel.kt:11)
    at com.apps.abousalem.movies.ui.MoviesViewModel.<init>(MoviesViewModel.kt:13)
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at java.lang.Class.newInstance(Class.java:1606)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:196)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:255)
    at androidx.lifecycle.ViewModelProvider$FactoryWrapper.create(ViewModelProvider.java:268)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:179)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:147)
    at com.apps.abousalem.movies.di.module.ActivityModule.provideMoviesViewModel(ActivityModule.kt:35)
    at com.apps.abousalem.movies.di.module.ActivityModule_ProvideMoviesViewModelFactory.proxyProvideMoviesViewModel(ActivityModule_ProvideMoviesViewModelFactory.java:27)
    at com.apps.abousalem.movies.di.module.ActivityModule_ProvideMoviesViewModelFactory.get(ActivityModule_ProvideMoviesViewModelFactory.java:18)
    at com.apps.abousalem.movies.di.module.ActivityModule_ProvideMoviesViewModelFactory.get(ActivityModule_ProvideMoviesViewModelFactory.java:8)
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
    at com.apps.abousalem.movies.di.component.DaggerMoviesActivityComponent.injectMainActivity(DaggerMoviesActivityComponent.java:55)
    at com.apps.abousalem.movies.di.component.DaggerMoviesActivityComponent.inject(DaggerMoviesActivityComponent.java:45)
    at com.apps.abousalem.movies.ui.MainActivity.onCreate(MainActivity.kt:28)
    at android.app.Activity.performCreate(Activity.java:6102)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2410)

这是ActivityModule,其中我提供了ViewModel ActivityModule

@Module
class ActivityModule(private val activity: AppCompatActivity){
@ActivityScope
@ActivityContext
@Provides
fun provideContext():Context{
    return activity
}
@ActivityScope
@ActivityContext
@Provides
fun provideActivity():AppCompatActivity{
    return activity
}
@ActivityScope
@Provides
fun provideMoviesViewModel(): MoviesViewModel {
    return ViewModelProviders.of(activity).get(MoviesViewModel::class.java)
}
@ActivityScope
@Provides
fun provideBaseViewModel(): BaseViewModel {
    return ViewModelProviders.of(activity).get(BaseViewModel::class.java)
}
}

在这里我提供Repository

应用程序模块

@Module
class ApplicationModule(var application: Application){

@ApplicationScope
@ApplicationContext
@Provides
fun provideContext(): Context {
    return application
}

@ApplicationScope
@ApplicationContext
@Provides
fun provideApplication(): Application {
    return application
}

@ApplicationScope
@Provides
fun getMovieDatabase(@ApplicationContext context: Application): MovieDatabase {
    return MovieDatabase.getInstance(context)
}

@ApplicationScope
@Provides
fun getSharedPreference(@ApplicationContext context: Context): SharedPreferences {
    return context.getSharedPreferences(MOVIES_PREFERENCE, Context.MODE_PRIVATE)
}

@ApplicationContext
@ApplicationScope
@Provides
fun getRepository(moviesApi: MoviesApi, movieDatabase: MovieDatabase, sharedPreferences: SharedPreferences): MoviesRepository {
    return MoviesRepository(moviesApi, movieDatabase, sharedPreferences)
}

}

在这里我注射它。

BaseViewModel

open class BaseViewModel: ViewModel(){
@Inject
lateinit var repository: MoviesRepository
}

我不知道为什么会这样,为什么无法初始化repositoryviewmodel

1 个答案:

答案 0 :(得分:0)

您注释了repository: MoviesRepository,将在注入时注入,但是您似乎从未实际注入viewmodel对象(ViewModelProviders仅提供并且不进行注入)。

您可以在匕首组件类中添加fun inject(viewModel: MyFinalViewModel)使其可注入,并调用component.inject(viewModel)注入它。
如果您不想注入视图模型(感觉有些明显),请尝试在其构造函数中传递依赖项。