Tag: 依赖注入

匕首2,不能在我的活动中注入演示者

我是Dagger2的新手。 我有一个注射主持人在我的活动中的问题我尝试解决我的问题后阅读本文https://android.jlelse.eu/inject-interfaces-without-providing-in-dagger-2-618cce9b1e29,但它不是帮助我。 我希望有人帮助我,我花了整天的时间来解决它… 这里是我的模块: @Module class AppModule(private val appContext: Context) { @Singleton @Provides internal fun provideContext(): Context { return appContext } @Singleton @Provides internal fun providePreferences(): SharedPreferences { return appContext.getSharedPreferences( appContext.resources.getString(R.string.shared_preferences_name), Context.MODE_PRIVATE) } } @Module abstract class ActivityModule { @Binds abstract fun provideMakeCheckPresenter (p : MakeCheckPresenter<MakeCheckMvpView>) : MakeCheckMvpPresenter<MakeCheckMvpView> } 这里是我的组件: @Component(modules = { AppModule.class, ActivityModule.class}) […]

如何使用Kodein依赖检索容器进行多重绑定?

我从Dagger 2迁移到Kodein,并希望替代多重绑定功能。 有一个吗?

Dagger2不能在andorid中工作

我有使用Dagger2的问题 我创建组件,模块,提供 class testModule { @Provides @Singleton fun provideTestServer(): TestService { } } 我在MainActivity中调用了onCreate() DaggerImageComponent.builder().build().inject(this) 这里是我的问题DI在MainActivity中正常工作 class MainActivity: AppCompatActivity { @Inject lateinit var testService: TestService } 但其他文件不工作。 object TestObject { @Inject @JvmSynthetic // error: static field cannot inject lateinit var testService: TestService fun test() = testService.testfun() } 要么 @Singleton class TestClass { @Inject lateinit var […]

建造工程时碰撞匕首

当我尝试构建生成Dagger文件的项目生成失败,出现异常。 你能告诉我如何修复它,或者至少在什么方向看。 e:/home/hodzi/Projects/StudioProjects/StackViewer/app/build/tmp/kapt3/stubs/debug/com/hodzi/stackviewer/di/AppComponent.java:6:error:[com.hodzi.stackviewer.questions。 di.QuestionsComponent] com.hodzi.stackviewer.questions.di.QuestionsComponent有冲突的范围:e: e:公共抽象接口AppComponent {e:^ e: com.hodzi.stackviewer.di.AppComponent也有@Singleton e:java.lang.IllegalStateException:未能分析:org.jetbrains.kotlin.kapt3.diagnostic.KaptError:在org.jetbrains.kotlin.analyzer的注解处理时出错。在org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler。)上的org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)处的AnalysisResult.throwIfError(AnalysisResult.kt:57) kt:154)at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)at org.jetbrains .kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)at org.jetbrains.kotlin.daemon.CompileServiceImpl $ compile在org.jetbrains.kotlin.daemon.CompileServiceImpl上编译$ 1 $ 2.invoke(CompileServiceImpl.kt:386) eServiceImpl $ doCompile $$ inlined $ ifAlive $ lambda $ 2.invoke(CompileServiceImpl.kt:889)at org.jetbrains.kotlin.daemon.CompileServiceImpl $ doCompile $$ inlined $ ifAlive $ lambda $ 2.invoke(CompileServiceImpl.kt:96)at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916)at org.jetbrains.kotlin.daemon.CompileServiceImpl。在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)上,在org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)处编译(CompileServiceImpl.kt:888)。 java:62)at sun.rlect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java :324)at sun.rmi.transport.Transport $ 1.run(Transport.java:200)at […]

kodein – 注入的数据值不是数据的值检索

在我的CallType类中,我把注入的数据connectivityState.callBackState为true,在ConnectivitySte类中初始化为false。 但是在我的IncomingCallBroadcastReceiver类中,我的布尔数据callBackState不是true而是false 。 class CallType { val kodein = Kodein { bind<ConnectivityState>() with provider { ConnectivityState() } } private val connectivityState: ConnectivityState = kodein.instance() fun call(number: String) { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\ connectivityState.callBackState = true } class IncomingCallBroadcastReceiver : KodeinBroadcastReceiver() { private val connectivityState: ConnectivityState by instance() override fun onBroadcastReceived(context: Context, intent: Intent) { //!!!!!!!!!!!!!! IT'S FALSE HERE […]

匕首2.11 – 如何让Activity和Fragment共享共同的价值观?

我目前正在学Android的Dagger 2,我想将Activity的注入注入到Fragment中,但我不知道如何。 这是一个工作设置,显示被注入片段和活动的值。 对不起,代码墙,我简化了我的代码,使其只包含相关的部分。 @Singleton @Component(modules = arrayOf(AndroidSupportInjectionModule::class, AppModule::class, BuildersModule::class)) interface AppComponent { @Component.Builder interface Builder { @BindsInstance fun setApplication(application: Application): Builder fun build(): AppComponent } fun apiService() : ApiService fun inject(app: App) } @Module(subcomponents = arrayOf(MainActivitySubComponent::class, MainFragmentSubComponent::class)) class AppModule @Module abstract class BuildersModule { @Binds @IntoMap @ActivityKey(MainActivity::class) abstract fun bindMainActivity(builder: MainActivitySubComponent.Builder): AndroidInjector.Factory<out Activity> @Binds […]

如何使@Autowired在普通班的工作?

这是我得到的: @Component class FooController { fun createFoo() { val foo = FooEntity() foo.name = "Diogo" fooRepository.save(foo) } @Autowired internal lateinit var fooRepository: FooRepository } 尝试调用createFoo() ,出现以下错误: kotlin.UninitializedPropertyAccessException: lateinit property fooRepository has not been initialized 我认为在顶部添加一个@Component会使我的类在Spring中被发现,从而使@Autowired能够工作,但也许我错了?

匕首2:具有相同键的多个条目

简短的解释 由Components体系结构描述的简化的UML图。 还有一个例外: java.lang.IllegalArgumentException: Multiple entries with same key: gson=com.example.di.AppPresentationComponent and gson=com.example.di.gamesession.GameSessionComponent Dagger 2无法决定从哪个Component提供一个Gson实例。 我永远不会使用多重继承。 你能推荐什么来解决我的情况? 我可以处理GameSessionComponent并将gameSessionManager()移动到GameSessionPresentationComponent 。 它会在我的情况下工作,但这听起来像一个肮脏的解决方案。 有一些代码。 AppComponent: @PerApplication @Component(modules = arrayOf( AppModule::class, TextsModule::class, AudioModule::class, FontsModule::class, TexturesModule::class, QuestModule::class, GameSaveModule::class )) interface AppComponent { fun componentsHolder(): ComponentsHolder fun gdxContext(): GdxContext fun rxHelper(): RxHelper fun textsManager(): TextsManager fun soundsManager(): AudioManager fun fontsManager(): FontsManager fun […]

@Inject设置不注入属性

我正在尝试使用Android Dagger‡实现,并将一个DispatchingAndroidInjector注入到我的Application类中: class MyApp : Application(), HasActivityInjector { private lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity> @Inject set override fun onCreate() { super.onCreate() AppInjector.init(this) } override fun activityInjector(): ActivityInjector<Activity> { return dispatchingAndroidInjector } } 但是我得到一个lateinit说明,说明“ lateinit属性没有被初始化”: 06-19 10:57:30.773 10797-10797/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.app, PID: 10797 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.ui.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property dispatchingAndroidInjector has […]

Dagger 2命名不能没有@Provides方法提供

试图咆哮匕首2,并与指定的提供商有问题。 我有一个简单的设置如下: // Module @Module class AppModule(private val app: App) { @Provides @AppScope fun providesApp() = app @Provides @AppScope fun provideSharedPreferences(app: App) = PreferenceManager.getDefaultSharedPreferences(app) @Provides @AppScope @Named("Uri1") fun providesUri1() = Uri.Builder().scheme("https").authority("authory1").build() @Provides @AppScope @Named("Uri2") fun providesUri2() = Uri.Builder().scheme("https").authority("authory2").build() } // Component @AppScope @Component(modules = arrayOf(AppModule::class)) interface AppComponent { fun inject(target: MainActivity) } // MainActivity @Inject […]