我在演示者中注入了Dagger2属性。
@Inject lateinit var dataInteractor: DataInteractor
可以通过两种方法访问它。在其中一个loadAppointments()
中,一切正常,但在另一个refund()
中,抛出了UninitializedPropertyAccessException
。该代码已经运行了一段时间,并且仅在几天前就出现了这个问题。
以前没有安装过Kotlin更新。
import android.util.Log
import co.example.Application
import co.example.domain.model.entity.AppointmentsEntity
import co.example.domain.model.entity.ProvidersEntity
import co.example.interactor.data.DataInteractor
import co.example.domain.di.base.RxDisposablePresenter
import co.example.view.operation.OperationView
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.*
import javax.inject.Inject
class AppointmentsPresenter : RxDisposablePresenter<AppointmentsPresenter.View>() {
@Inject
lateinit var dataInteractor: DataInteractor
private lateinit var operationView: OperationView<*>
private val results: ArrayList<AppointmentsEntity.AppointmentEntity> = ArrayList()
private val resultsHistory: ArrayList<AppointmentsEntity.AppointmentEntity> = ArrayList()
override fun onTakeView(view: View?) {
super.onTakeView(view)
operationView = view?.operationView()!!
}
fun loadAppointments() {
val userID = Application.appComponent.userInternalStorage().userID()
if (userID != null) {
add(dataInteractor
.getAppointments(userID, LIMIT, OFFSET)
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { operationView.showProgress() }
.doFinally {
operationView.hideProgress()
view?.onAppointmentsDataFilled(results, resultsHistory)
}
.subscribe(
{
divideUpcomingAndCompleteAppointments(it)
view?.unblockAppointments()
},
{
val message = it.message
if (message!!.contains("blocked")) {
view?.blockAppointments()
operationView.showError("Error")
return@subscribe
}
operationView.showError(message)
}
))
}
}
fun refund(position: Int?) {
add(dataInteractor
.refund(results[position!!].id!!)
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { operationView.showProgress() }
.doFinally {
operationView.hideProgress()
}
.subscribe(
{
view?.onRefundCompleted()
},
{
val message = it.message
operationView.showError(message!!)
}
))
}
private fun divideUpcomingAndCompleteAppointments(appointmentsEntity: AppointmentsEntity) {
results.clear()
resultsHistory.clear()
for (appointment in appointmentsEntity.results!!) {
if (appointment.status.equals(STATUS_COMPLETE, true)) {
resultsHistory.add(appointment)
} else {
results.add(appointment)
}
}
}
fun loadProviderById(doctorId: Long?, onDoctorRetrivedListener: (ProvidersEntity.ProviderEntity) -> Unit) {
add(
dataInteractor
.getProviders(0, 100, 0, 0)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ providersEntity ->
val doctor = providersEntity
.results
?.filter {
Log.d("ATAT", "loadProviderById: ${it.user_id}")
it.user_id == doctorId
}
doctor?.let {
if (it.isNotEmpty()) {
onDoctorRetrivedListener.invoke(doctor[0])
}
}
},
{
val message = it.message
operationView.showError(message!!)
}))
}
interface View {
fun operationView(): OperationView<*>
fun onAppointmentsDataFilled(appointments: ArrayList<AppointmentsEntity.AppointmentEntity>?,
appointmentsHistory: ArrayList<AppointmentsEntity.AppointmentEntity>?)
fun onRefundCompleted()
fun blockAppointments()
fun unblockAppointments()
}
companion object {
const val STATUS_COMPLETE = "Complete"
const val LIMIT: Long = 1000
const val OFFSET: Long = 0
}
}
这是堆栈跟踪:
2018-12-13 11:59:29.359 4808-4808/co.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: co.example, PID: 4808
kotlin.UninitializedPropertyAccessException: lateinit property dataInteractor has not been initialized
at co.example.presenter.appointements.AppointmentsPresenter.refund(AppointmentsPresenter.kt:59)
at co.example.view.appointements.AppointmentsFragment.refundRequest(AppointmentsFragment.kt:123)
at co.example.activity.MainActivity.onRefundRequested(MainActivity.kt:295)
at co.example.view.dialog.RefundDialogFragment$onCreateDialog$1.onClick(RefundDialogFragment.kt:16)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
UPD: 如果将调用这些方法的片段创建为单例,则不会重现。