我正在尝试使用匕首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)?如果您对问题有任何疑问,请在下面评论。谢谢。
object
(Singleton)?