由于某些设备上发生的运行时错误,我需要能使我的类可为空/可选的东西。
这可能吗?
class MyFragment extends Fragment {
@Inject
var presenter: MyPresenter? = null
// Other codes here...
}
我想使用presenter
作为选项,因为旧的Android设备的 SOME 特别是抛出此错误(这是在我删除了lazyinit之前)。
代码:
class MyFragment extends Fragment {
@Inject
lazyinit var presenter: MyPresenter? = null
// Other codes here...
override fun onDestroy() {
super.onDestroy()
presenter.somecode()
}
}
错误:
Fatal Exception: java.lang.RuntimeException: Unable to destroy activity {com.sample.MyActivity}: c.r: lateinit property presenter has not been initialized
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3497)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3515)
at android.app.ActivityThread.access$1400(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1249)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by c.r: lateinit property presenter has not been initialized
at com.sample.MyFragment.onDestroy(SourceFile:459)
at android.support.v4.app.Fragment.performDestroy(SourceFile:2434)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1442)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1595)
at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(SourceFile:2951)
at android.support.v4.app.FragmentController.dispatchDestroy(SourceFile:271)
at android.support.v4.app.FragmentActivity.onDestroy(SourceFile:390)
at android.support.v7.app.AppCompatActivity.onDestroy(SourceFile:209)
at android.app.Activity.performDestroy(Activity.java:5403)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3484)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3515)
at android.app.ActivityThread.access$1400(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1249)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(NativeStart.java)
这种情况是否有办法或解决方法?
答案 0 :(得分:27)
我遇到了同样的问题并且这样解决了:
@Inject
@JvmField
var presenter: Presenter? = null
希望它有所帮助。
答案 1 :(得分:0)
如果您没有需要访问presenter
字段的Java代码,则无需使用@JvmField
对其进行注释。相反,您可以像这样声明您的字段:
@set:Inject
var presenter: Presenter? = null