我正在使用 koin 学习 kotlin。在目录中运行应用程序时,我看到以下消息。
java.lang.IllegalStateException:KoinApplication 尚未启动
虽然我在 MyApplication 中使用了startKoin
class MyApplication : Application() {
var listOfModules = module {
single { GitHubServiceApi() }
}
override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@MyApplication)
modules(listOfModules)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现了我犯了错误的问题。我应该在 mainfest.xml 中添加 MyApplcation 名称
小智 18
在清单文件中添加“android:name=”.TheApplication”解决了该问题。
android:name=".TheApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_app_icon_round"
android:supportsRtl="true"
android:theme="@style/Theme.Shrine">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
"android:name=".TheApplication" 是 Koin 中的类名
class TheApplication : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
startKoin {
androidLogger()
androidContext(androidContext = this@TheApplication)
modules(
listOfModules
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的manifest.xml中,
<application>
android:name=".MyApplication"
...
</application
Run Code Online (Sandbox Code Playgroud)
在应用程序标签中添加此行。
基本上,您需要在清单中提供调用 startKoin() 方法的类的名称作为应用程序名称。这将让您配置日志记录、属性加载和模块。看看这个: https: //doc.insert-koin.io/#/koin-core/dsl
| 归档时间: |
|
| 查看次数: |
5976 次 |
| 最近记录: |