我试着做一个迷你项目(stopWatch)这是我在网上看到的这个项目https://github.com/philipplackner/StopWatchComposeDesktop的源代码。我没有任何编译问题,但我有一个运行时问题。我比较了我的源文件和他的源文件,它们是相等的。但是构建文件是不同的,这是我的项目https://github.com/victorYghor/StopWatch3。问题是,当我尝试点击一个按钮时,我会遇到以下问题:
enter image description here
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelled}@19d52b0, Dispatchers.Main[missing, cause=java.lang.NoClassDefFoundError: android/os/Looper]]
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.awt.ComposeLayer$coroutineExceptionHandler$1@786a520b, androidx.compose.runtime.BroadcastFrameClock@46a56726, StandaloneCoroutine{Cancelling}@36a64531, FlushCoroutineDispatcher@657df46]
这就是工作的构建。gradle。kts
import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.5.31"
id("org.jetbrains.compose") version "1.0.0"
}
group = "me.philipplackner"
version = "1.0"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
dependencies {
implementation(compose.desktop.currentOs)
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "StopWatchComposeDesktop"
packageVersion = "1.0.0"
}
}
}
下面你可以看到我的build.gradle.kts不工作
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}
group = "com.example"
version = "1.0-SNAPSHOT"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
kotlin {
jvm {
jvmToolchain(11)
withJava()
}
sourceSets {
val jvmMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-javafx")
implementation(compose.desktop.currentOs)
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-android:0.30.1")
}
}
val jvmTest by getting
}
}
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "StopWatch"
packageVersion = "1.0.0"
}
}
}
我想知道,如何可能更新我的构建文件,因为我没有更多的运行时问题。
我相信我需要改变一些参数在gradle构建文件。另一个可能有帮助的地方是,问题开始于StopWatch.kt的第22行
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import kotlinx.coroutines.*
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*
class StopWatch {
var formattedTime by mutableStateOf("00:00:000")
private var coroutineScope = CoroutineScope(Dispatchers.Main)
private var isActive = false
private var timeMillis = 0L
private var lastTimestamp = 0L
fun start() {
if (isActive) return
coroutineScope.launch { \\ THE PROBLEM START HERE
lastTimestamp = System.currentTimeMillis()
this@StopWatch.isActive = true
while(this@StopWatch.isActive) {
delay(10L)
timeMillis += System.currentTimeMillis() - lastTimestamp
lastTimestamp = System.currentTimeMillis()
formattedTime = formatTime(timeMillis)
}
}
}
fun pause() {
isActive = false
}
fun reset() {
coroutineScope.cancel()
coroutineScope = CoroutineScope(Dispatchers.Main)
timeMillis = 0L
formattedTime = "00:00:000"
isActive = false
}
private fun formatTime(timeMillis: Long): String {
val localDateTime = LocalDateTime.ofInstant(
Instant.ofEpochMilli(timeMillis),
ZoneId.systemDefault()
)
val formatter = DateTimeFormatter.ofPattern(
"mm:ss:SSS",
Locale.getDefault()
)
return localDateTime.format(formatter)
}
}```
1条答案
按热度按时间rdlzhqv91#
对我来说,解决这个问题的不是使用调度程序。主要的,由于某种原因,错误是从这个到达的。如果我为我的协程创建一个专用线程,一切正常,我使用
Dispatchers.Default