Dark 2在Kotlin对象中注入上下文

问题描述 投票:0回答:2

我正在尝试使用匕首2注入Context

AppComponent.kt:

@Singleton
@Component(
    modules = [
        AppModule::class
    ]
)
interface AppComponent {
    fun context(): Context
}

AppModule.kt:

@Module
class AppModule(private val application: Application) {

    @Provides
    @Singleton
    fun providesApplicationContext(): Context = application
}

MainApp.kt:

class MainApp : Application() {
    lateinit var appComponent: AppComponent

    override fun onCreate() {
        super.onCreate()
        appComponent = initDagger()
    }

    private fun initDagger() = DaggerAppComponent.builder()
        .appModule(AppModule(this))
        .build()
}

Manager.kt :(我想在其中注入Context的类)

object Manager {

    @Inject
    lateinit var context: Context
}

但是,我遇到以下错误:

error: Dagger does not support injection into static fields
    public static android.content.Context context;
                                          ^

这是因为我使用的是object(Singleton)?如果您对问题有任何疑问,请在下面评论。谢谢。

android kotlin dependency-injection dagger-2
2个回答
0
投票
这是因为我正在使用object(Singleton)?

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.