当我注入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)
这是我提供ViewModel
的ActivityModule
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
}
我无法弄清楚为什么会发生这种情况,为什么不能初始化repository
和viewmodel
您注释您的repository: MoviesRepository
在注入时注入,但是您似乎从来没有实际注入ViewModel对象(ViewModelProviders
仅提供并且不提供注入)。
您可以使其在dagger组件类中注射添加fun inject(viewModel: MyFinalViewModel)
,并注入调用component.inject(viewModel)
。
如果您不想注入ViewModels(感觉有点明确),请尝试通过其构造函数中的依赖项。