面对问题在 AppModule 中插入 ContentProvider。

我的应用级模块是

module {
        single { TinyDB.getInstance(get()) }
        single { DataRepository(get()) }
        viewModel { HomeViewModel(get()) }
    }

我不知道如何在模块中使用 ContentProvider

我试过代码
val dataRepo = inject(DataRepository::class.java)

这在我的内容提供程序中,但在此之后我的应用程序开始崩溃
和 logcat 显示错误按摩
    java.lang.RuntimeException: Unable to get provider wastickerapps.newemojistickers.stickersforwhatsapp.whatsappUsages.StickerContentProvider: java.lang.IllegalStateException: StandAloneContext Koin instance is null
        at android.app.ActivityThread.installProvider(ActivityThread.java:5562)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5154)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5094)
        at android.app.ActivityThread.access$1600(ActivityThread.java:177)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5942)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.IllegalStateException: StandAloneContext Koin instance is null
        at org.koin.standalone.StandAloneContext.getKoin(StandAloneContext.kt:68)
        at org.koin.standalone.KoinComponentKt.getKoinContext(KoinComponent.kt:42)
        at org.koin.standalone.KoinComponentKt.access$getKoinContext(KoinComponent.kt:1)
        at org.koin.standalone.KoinComponent$DefaultImpls.getKoin(KoinComponent.kt:36)
        at wastickerapps.newemojistickers.stickersforwhatsapp.whatsappUsages.StickerContentProvider.getKoin(StickerContentProvider.kt:27)
        at wastickerapps.newemojistickers.stickersforwhatsapp.whatsappUsages.StickerContentProvider.<init>(StickerContentProvider.kt:481)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1650)
        at android.app.ActivityThread.installProvider(ActivityThread.java:5547)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5154) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5094) 
        at android.app.ActivityThread.access$1600(ActivityThread.java:177) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:145) 
        at android.app.ActivityThread.main(ActivityThread.java:5942) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at

最佳答案

内容提供者初始化在 Application() 类中的 onCreate() 方法之前,我已经从 Application() 类中的 attachBaseContext() 方法调用 startKoin 在我的 ContentProvider 类中实现 KoinComponent
并使用此在 ContentProvider 中注入(inject)数据存储库

 var dataRepo = inject<DataRepository>()

这是注入(inject)的代码
inline fun <reified T : Any> inject(name: String = "", scope: Scope? =
null, noinline parameters: ParameterDefinition =
emptyParameterDefinition()) = lazy { get<T>(name, scope, parameters) }

inline fun <reified T : Any> get(name: String = "", scope: Scope? =
null, noinline parameters: ParameterDefinition = e .
mptyParameterDefinition()): T = getKoin().get(name, scope, parameters)
//
fun getKoin(): KoinContext =
(org.koin.standalone.StandAloneContext.koinContext as KoinContext)

关于android - 如何使用 insert-koin 在 Content Provider 中注入(inject) datarepository 实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56003069/

10-11 22:43
查看更多