gpt4 book ai didi

Kotlin 和 Dagger : Can I use @Inject to an object still make it nullable/optional?

转载 作者:IT老高 更新时间:2023-10-28 13:39:06 26 4
gpt4 key购买 nike

由于某些设备上发生的运行时错误,我需要一些可以使我的类为空/可选的东西。

这可能吗?

class MyFragment extends Fragment {
@Inject
var presenter: MyPresenter? = null
// Other codes here...
}

我想使用 presenter 作为选项,因为 某些 旧的 Android 设备尤其会抛出此错误(这是在我删除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)

这种情况有办法或解决方法吗?

最佳答案

我遇到了同样的问题并这样解决:

@Inject
@JvmField
var presenter: Presenter? = null

希望对你有帮助。

关于Kotlin 和 Dagger : Can I use @Inject to an object still make it nullable/optional?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44310196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com