Kotlin and Dagger: Can I use @Inject to an object

2020-02-25 08:35发布

问题:

I need something that can make my class nullable/optional because of the runtime errors that is happening on some devices.

Is this possible?

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

I wanted to use the presenter as an option because SOME of the old Android devices especially are throwing this error (this is before I removed the lazyinit).

Code:

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

     override fun onDestroy() {
        super.onDestroy()
        presenter.somecode()
    }
}

Error:

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)

Is there are way or workaround for this situation?

回答1:

I had the same problem and solved like this:

@Inject
@JvmField
var presenter: Presenter? = null

Hope it helps.



回答2:

If you don't have Java code that needs to access the presenter field you don't need to annotate it with @JvmField. Instead, you can declare your field like so:

@set:Inject
var presenter: Presenter? = null