Dagger 2,在模块中提供应用程序上下文 [英] Dagger 2, Providing Application Context in Module
问题描述
我在Android开发中是相当新的,在DI中则是新的.我在一个个人项目上使用Kotlin,在该项目上我尝试使用Dagger2.我设法将其设置为util类,但是在这里我需要有一个上下文才能使用它来注入需要上下文的类(sharedpref管理器)课),我失败了.这是我的代码,这是我遇到的错误(NPE).预先谢谢你.
Im pretty new in Android development and newer in DI. I am using Kotlin on a personal project where I am experimenting with Dagger 2. I managed to set it up for a util class, but where i need to have a context to use it to inject a class that requires a context (a sharedpref manager class), I failed. Here is my code, and here is the error ( NPE ) i am getting. Thank you in advance.
我的模块类
package com.android.pine
import android.content.Context
import com.android.pine.utils.SharedPreferencesManager
import dagger.Module
import dagger.Provides
import javax.inject.Singleton
@Module
class AppModule {
@Provides
@Singleton
fun context(pineApplication: PineApplication): Context = pineApplication.applicationContext
@Provides
@Singleton
fun provideSharedPrefManager(context: Context): SharedPreferencesManager = SharedPreferencesManager(context)
}
我的组件类:
package com.android.pine
import com.android.pine.home.HomePresenter
import com.android.pine.home.categories.CategoryAdapter
import dagger.Component
import javax.inject.Singleton
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
fun inject(categoryAdapter: CategoryAdapter)
fun inject(homePresenter: HomePresenter)
}
添加了以下信息, 我如何称呼sharedPreferencesManager的注入:
edit: added the info below, How I call the inject of sharedPreferencesManager:
class HomePresenter : BasePresenter<HomeView>() {
@Inject
lateinit var sharedPreferencesManager: SharedPreferencesManager
.
.
.
也在我的HomePresenter类中,在onAttached方法中覆盖:
Also in my HomePresenter class, in onAttached method override:
DaggerAppComponent.create().inject(this)
我的pineApplication类和SharedPrefManager类如下:
My pineApplication class and SharedPrefManager class looks like this:
class PineApplication @Inject constructor(): Application()
SharedPref:
SharedPref:
class SharedPreferencesManager @Inject constructor(context: Context) {
.
.
.
崩溃,无法获取pineApplication.getContext()(已编辑,已添加完整堆栈跟踪)
Crash, can not get the pineApplication.getContext() (edited, added the full stack trace)
06-02 11:57:01.028 14840-14840/com.android.pine.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.pine.debug, PID: 14840
java.lang.RuntimeException: Unable to resume activity {com.android.pine.debug/com.android.pine.home.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3429)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
at com.android.pine.AppModule.context(AppModule.kt:12)
at com.android.pine.AppModule_ContextFactory.proxyContext(AppModule_ContextFactory.java:34)
at com.android.pine.DaggerAppComponent.getContext(DaggerAppComponent.java:29)
at com.android.pine.DaggerAppComponent.getSharedPreferencesManager(DaggerAppComponent.java:34)
at com.android.pine.DaggerAppComponent.injectHomePresenter(DaggerAppComponent.java:59)
at com.android.pine.DaggerAppComponent.inject(DaggerAppComponent.java:49)
at com.android.pine.home.HomePresenter.onAttached(HomePresenter.kt:31)
at com.android.pine.home.HomePresenter.onAttached(HomePresenter.kt:10)
at com.android.pine.core.BaseActivity.onResume(BaseActivity.kt:34)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
at android.app.Activity.performResume(Activity.java:6783)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3406)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
推荐答案
您不能使用class PineApplication @Inject constructor(): Application()
创建PineApplication
.这是一个框架类,必须由Android框架创建.
You can't use class PineApplication @Inject constructor(): Application()
to create PineApplication
. It's a framework class and has to be created by the Android Framework.
这样做Dagger将创建PineApplication
,但是applicationContext
将返回null
,因为它从未被系统初始化过.
Doing so Dagger will create PineApplication
, but applicationContext
will return null
as it has never been initialized (by the system).
不要对框架类使用构造函数注入,也不要自己创建.使用@Bindsintance
将对象及其生成器添加到组件中,或使用模块来提供它.
Don't use constructor injection for framework classes and don't create the yourself. Use @Bindsintance
to add the object to the component with its builder, or use a module to provide it.
这篇关于Dagger 2,在模块中提供应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!