0

我有两个文件one.kttwo.kt我在one.kt文件中有内部类。我想在执行某些操作时调用该内部类,例如在 Two.kt 文件中单击按钮。我尝试了一些调用内部类的方法,但方法不调用。请指导我我有什么错误。来自文件的One.kt文件调用manifest。像这样,

<application
        android:name="com.abc.cde.One"

一.kt:

open class One : MultiDexApplication(), KodeinAware {
    ---------------
    --------------
    OnCreate()
    ---------------
    ---------------

inner class CallmyClass() {

        fun CallMyMethod(i: Int) :Module {

            return Module {
                bind<ExchangeRateProvider>() with singleton { CryptoCompareExchangeProvider(this@One, instance()) }
                bind<SyncProgressProvider>() with singleton { SyncProgressProvider() }
                bind<WallethKeyStore>() with singleton { keyStore }
                bind<Settings>() with singleton { KotprefSettings }

                bind<CurrentTokenProvider>() with singleton { CurrentTokenProvider(instance()) }

                bind<AppDatabase>() with singleton { Room.databaseBuilder(applicationContext, AppDatabase::class.java, "maindb").build() }
                bind<NetworkDefinitionProvider>() with singleton { NetworkDefinitionProvider(instance()) }
                //bind<CurrentAddressProvider>() with singleton { InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext) }
                bind<CurrentAddressProvider>() with singleton { InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,i) }

                bind<FourByteDirectory>() with singleton { FourByteDirectoryImpl(instance(), applicationContext) }
            }
        }
    }
}

二.kt:

class Two : AppCompatActivity(), KodeinAware {

    -----------
    -----------
    -----------

    oncreate()

    -------------
    --------------

     val dd = One().CallmyClass().CallMyMethod(1)
}

一些博客说你可以使用它来调用这种类型的方法,但我不知道如何在 Activity 类中使​​用,因为它没有扩展 Application 类。

    override val kodein = Kodein.lazy {
           import(CallMyMethod(1))
    }

我将此代码包含在我的 One.kt 文件中,我想从 Two.kt 文件中调用是否可能?请告诉我

这样对吗 ?像这样调用 val dd = One().CallmyClass().CallMyMethod(1)。但它没有调用我的方法,因为上面的方法是使用 KodeinAware 返回模块类型。

参考:https ://proandroiddev.com/dependency-injection-with-kotlin-kodein-koin-3d783745e48d

更新:

private fun CallMyMethod(i: Int) :Module {
                return Module {

                    //bind<ExchangeRateProvider>() with singleton { CryptoCompareExchangeProvider(this@App, instance()) }
                    bind<ExchangeRateProvider>(overrides = true) with singleton { CryptoCompareExchangeProvider(this@App, instance()) }

                    //bind<SyncProgressProvider>() with singleton { SyncProgressProvider() }
                    bind<SyncProgressProvider>(overrides = true) with singleton { SyncProgressProvider() }

                    //bind<WallethKeyStore>() with singleton { keyStore }
                    bind<WallethKeyStore>(overrides = true) with singleton { keyStore }

                    //bind<Settings>() with singleton { KotprefSettings }
                    bind<Settings>(overrides = true) with singleton { KotprefSettings }

                    //bind<CurrentTokenProvider>() with singleton { CurrentTokenProvider(instance()) }
                    bind<CurrentTokenProvider>(overrides = true) with singleton { CurrentTokenProvider(instance()) }

                    //bind<AppDatabase>() with singleton { Room.databaseBuilder(applicationContext, AppDatabase::class.java, "maindb").build() }
                    bind<AppDatabase>(overrides = true) with singleton { Room.databaseBuilder(applicationContext, AppDatabase::class.java, "maindb").build() }

                    //bind<NetworkDefinitionProvider>() with singleton { NetworkDefinitionProvider(instance()) }
                    bind<NetworkDefinitionProvider>(overrides = true) with singleton { NetworkDefinitionProvider(instance()) }

                    //bind<CurrentAddressProvider>() with singleton { DuplicateInitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,1) }
                    bind<CurrentAddressProvider>(overrides = true) with singleton { DuplicateInitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,1) }

                    //bind<FourByteDirectory>() with singleton { FourByteDirectoryImpl(instance(), applicationContext) }
                    bind<FourByteDirectory>(overrides = true) with singleton { FourByteDirectoryImpl(instance(), applicationContext) }
                }
            }

            override val kodein = Kodein {
                //import(CallMyMethod(1), allowOverride = true)
                import(CallMyMethod(1))
            }
4

0 回答 0