1

koin 在我的大多数相同型号的设备上都可以正常工作,但它会导致具有 IllegalStateException 的单个设备崩溃,我已将 android 上下文作为 applicationContext 但它仍然会导致崩溃。

在更新到最新版本之前,它抛出“由:java.lang.IllegalStateException:KoinApplication 尚未启动”但更新后

E/AndroidRuntime: FATAL EXCEPTION: main
Process: #######, PID: 31641
java.lang.RuntimeException: Unable to start activity ComponentInfo{###.activities.SplashScreenActivity}: java.lang.IllegalStateException: No Koin Context configured. Please use startKoin or koinApplication DSL.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3044)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3122)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6892)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
Caused by: java.lang.IllegalStateException: No Koin Context configured. Please use startKoin or koinApplication DSL.
at org.koin.core.context.KoinContextHandler.getContext(KoinContextHandler.kt:29)
at org.koin.core.context.KoinContextHandler.get(KoinContextHandler.kt:35)
at org.koin.android.ext.android.ComponentCallbackExtKt.getKoin(ComponentCallbackExt.kt:15)
at org.koin.androidx.viewmodel.ext.android.ViewModelStoreOwnerExtKt.getKoin(ViewModelStoreOwnerExt.kt:63)
at org.koin.androidx.viewmodel.ext.android.ViewModelStoreOwnerExtKt.getViewModel(ViewModelStoreOwnerExt.kt:60)
at .SplashScreenActivity$$special$$inlined$viewModel$1.invoke(ViewModelStoreOwnerExt.kt:45)
at #######.activities.SplashScreenActivity$$special$$inlined$viewModel$1.invoke(Unknown Source:0)
at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
at #########activities.SplashScreenActivity.getMViewModel(Unknown Source:7)
at #########.activities.SplashScreenActivity.onCreate(SplashScreenActivity.kt:86)
at android.app.Activity.performCreate(Activity.java:7147)
at android.app.Activity.performCreate(Activity.java:7138)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1219)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2997)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3122) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:192) 
at android.app.ActivityThread.main(ActivityThread.java:6892) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875) 

我在应用程序类开始 koin:

startKoin { // 声明使用的 Android 上下文 androidContext(this@QairosApplication) // 声明模块 modules(myModule) }

4

1 回答 1

8

在清单文件中,您需要提供使用 startKoin{} 的类的名称。例如,如果您在一个名为 BaseApplication 的类中启动 Koin,那么在您的清单中,它应该如下所示:

 <application
    android:name=".BaseApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

在 BaseApplication 类中:

class BaseApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidLogger()
            androidContext(this@BaseApplication)
            modules(listOf(module1, module2))
        }
}
于 2020-09-13T13:00:02.063 回答