I am new in android kotlin and dagger,I am facing above issue. I already provide the ViewModelFactory but facing issue i don't know why this issue is came, i have attached the MainActivity, Repository class and Viewmodel class,Please resolve my issue what I am take my mistake, Please help.
//MainActivity.class
`class MainActivity : AppCompatActivity() {
@Inject
lateinit var viewModelFactory: ViewModelFactory
private lateinit var reportViewModel: ReportsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//(application as PWEApplication).daggerComponent.inject(this)
reportViewModel = ViewModelProvider(this, viewModelFactory).get(ReportsViewModel::class.java)
reportViewModel.report.observe(this, Observer {
when(it.status){
NetworkResource.Status.LOADING -> {
// Show a loading indicator
}
NetworkResource.Status.SUCCESS -> {
// Show the data from the API
Log.e("installationReport",""+it.data)
}
NetworkResource.Status.ERROR -> {
// Show an error message
}
}
})
}
}`
//Viewmodel.class
``open class ReportsViewModel @Inject constructor(private val repository: ReportsRepository) :ViewModel() {
private var reportLiveData = MediatorLiveData<ReportRequest>()
val report = Transformations.switchMap(reportLiveData) {
repository.getReports(it)
}
fun getInstallReport(idd: Int, fromDate: String, toDate: String) {
reportLiveData.value = ReportRequest(idd, fromDate, toDate)
}
}`
//ReportRepository.class
class ReportsRepository @Inject constructor(
private val reportServiceNS: ReportsService,
private val schedulerProvider: SchedulerProvider
) {
private val reportsMutableLiveData = MutableLiveData<NetworkResource<List<ResponseItem>>>()
private var disposable: Disposable? = null
fun getReports(reportRequest: ReportRequest): MutableLiveData<NetworkResource<List<ResponseItem>>>{
disposable = reportServiceNS.getReport(
reportRequest.script,
reportRequest.deploy,
reportRequest.compId,
reportRequest.h,
reportRequest.discId,
reportRequest.fromDate,
reportRequest.toDate)
.compose(schedulerProvider.getSchedulersForObservable())
.subscribeWith(object : DisposableObserver<ReportResponse>() {
override fun onComplete() {
}
override fun onNext(t: ReportResponse) {
reportsMutableLiveData.value = NetworkResource.success(t.complaintData)
}
override fun onError(e: Throwable) {
reportsMutableLiveData.value = NetworkResource.error(e, null)
}
})
return reportsMutableLiveData
}
}`
You mentioned you are using dagger. I see you are expecting the following to be injected, but doing nothing for that to happen:
@Inject
lateinit var viewModelFactory: ViewModelFactory
So I can imagine your app is crashing because viewModelFactory
is not initialized. Depending on your DI structure, you should either do component/subcomponent injection, or use AndroidInjection.inject(this)