我正在尝试添加协程,该协程首先将progressBar
设置为可见,然后向服务器请求数据,然后在获取数据时将progressBar
设置为不可见。我已经读过要与UI进行交互,我的协程需要在Dispatcher.Main
中进行操作,但是当我尝试设置launch(Dispatcher.Main)
时,整个应用程序会无错误终止。
我从https://www.kotlindevelopment.com/deep-dive-coroutines/开始了以下教程。我从此处显示的代码中更改了一部分:
launch(UI) {
progressBar.visibility = View.VISIBLE
try {
val userString = fetchUserString("1").await()
val user = deserializeUser(userString).await()
showUserData(user)
} catch (ex: Exception) {
log(ex)
} finally {
progressBar.visibility = View.GONE
}
}
收件人:
GlobalScope.launch(Dispatchers.Main) {
progressBarMarkers.visibility = View.VISIBLE
try {
val repository = MarkerRepository()
points = repository.getAllDataAsync().await()
}
} catch (ex: Exception) {
Log.d("EXCEPTION", ex.toString())
} finally {
progressBarMarkers.visibility = View.INVISIBLE
}
}
但是没有用。我开始搜索可能是什么问题,然后发现当协程如下所示时,应用程序到达withContext(Dispatchers.Main)
GlobalScope.launch{
val button = findViewById<ProgressBar>(R.id.progressBarMarkers)
withContext(Dispatchers.Main){
button.visibility = View.VISIBLE
}
}
对于Kotlin和协程,我还是很陌生,所以也许这只是一些基本错误,但是我找不到为什么应用程序终止的答案,还有什么可以终止而没有错误
整个协程都在:
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
private val REQUEST_PERMISSION_CODE: Int = 123
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
ShowPlacesButton.setOnClickListener {
launch{
val button = findViewById<ProgressBar>(R.id.progressBarMarkers)
withContext(Dispatchers.Main + Job()){
button.visibility = View.VISIBLE
}
}
}
}
我的gradle文件的一部分:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"
implementation 'com.google.dagger:dagger:2.13'
kapt 'com.google.dagger:dagger-compiler:2.13'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
为什么应用程序到达Dispatcher.Main
时为什么终止?
答案 0 :(得分:1)
您可能在logcat中启用了“仅显示选定的应用程序”吗?还是其他某种过滤器?
因为我在运行代码时遇到了非常有用的崩溃:
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.idunnno.test, PID: 27501
java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android'
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:73)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:54)
at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:373)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:25)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:152)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source:1)
at com.idunnno.daggertest.MainActivity$onCreate$1$1.invokeSuspend(MainActivity.kt:27)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
基本修复方法是将此行添加到您的Gradle依赖项中:
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"
其他一些注意事项:
findViewById
。您应该一次执行一次任务-可能在onCreate
中执行-然后将其保存到类级属性中。launch
。CoroutineScope
。这意味着您不必每次都要使用GlobalScope
或launch
时都创建新的上下文或使用async
。答案 1 :(得分:0)
将此行添加到您的proguard文件中
-keep类kotlinx.coroutines.android。* {*;}
答案 2 :(得分:0)
这与我在使用 Retrofit 从协程作用域内的 Internet 获取一些数据时遇到的错误相同。
修复非常简单,就是在我的测试物理设备上打开互联网。