Kotlin中出现未知错误:kotlin.NotImplementedError:仅在JetBrains的分叉版本中实现,有人能帮我解决吗?
我在Kotlin项目里遇到了一个自己也不太清楚原因的错误。
这个项目原本可以跑着,但后来我在另一台电脑上克隆了一份,结果在那台电脑上出现了下面这个错误:
FATAL EXCEPTION: main (Ask Gemini)
Process: sguichard.myapplication, PID: 16760
kotlin.NotImplementedError: Implemented only in JetBrains fork.
at androidx.navigation.compose.NavHostController_nonAndroidKt.rememberNavController(NavHostController.nonAndroid.kt:32)
at sguichard.myapplication.MainActivityKt.MainActivityContent(MainActivity.kt:69)
at sguichard.myapplication.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:58)
at sguichard.myapplication.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:57)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:441)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:259)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:258)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:216)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:132)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:131)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:121)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:155)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:154)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:401)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:154)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:133)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:97)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3595)
at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:3522)
at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:743)
at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:1122)
at androidx.compose.runtime.CompositionImpl.composeInitial(Composition.kt:649)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:635)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:133)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:124)
at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:1626)
at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:124)
2026-03-18 09:59:23.819 16760-16760 AndroidRuntime sguichard.myapplication E at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:180) (Ask Gemini)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.jvm.kt:313)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.jvm.kt:191)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:124)
at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1707)
at android.view.View.dispatchAttachedToWindow(View.java:20753)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3490)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2613)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2126)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8653)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1037)
at android.view.Choreographer.doCallbacks(Choreographer.java:845)
at android.view.Choreographer.doFrame(Choreographer.java:780)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1022)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
下面是我的MainActivity.kt代码(其他部分不重要):
package sguichard.myapplication
import android.app.AlertDialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CardColors
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.launch
import sguichard.myapplication.api.CarShareAreaApiManager
import sguichard.myapplication.model.AllCarShareArea
import sguichard.myapplication.model.CarShareArea
import sguichard.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MainActivityContent()
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainActivityContent(){
var currentPage by remember { mutableStateOf(NavigationKeys.PAGE_1) }
val context = LocalContext.current
var startDestination = NavigationKeys.PAGE_1
val navController = rememberNavController()
var selectedDestination by rememberSaveable{
mutableIntStateOf(startDestination.ordinal)
}
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
TopAppBar(
title = {
Text(
text = when(currentPage){
NavigationKeys.PAGE_1 -> NavigationKeys.PAGE_1.title
NavigationKeys.PAGE_2 -> NavigationKeys.PAGE_2.title
else -> ""
}
)
}
)
},
bottomBar = {
NavigationBar {
NavigationKeys.entries.forEach {
NavigationBarItem(
selected = selectedDestination == it.ordinal,
onClick = {
navController.navigate(route = it.name)
selectedDestination = it.ordinal
currentPage = it
},
icon = { Icon(it.icon, it.contentdescription) },
label = { Text(it.title) }
)
}
}
}
) { innerPadding ->
NavHost(
navController,
startDestination = startDestination.name
){
NavigationKeys.entries.forEach { destination -> composable (destination.name){
when (destination) {
NavigationKeys.PAGE_1 -> CarShareAreaScreen(
modifier = Modifier.padding(innerPadding)
)
NavigationKeys.PAGE_2 -> MapScreen()
else -> {}
}
}
}
}
}
}
@Composable
fun MapScreen(modifier: Modifier = Modifier){
Text("Map")
}
@Composable
fun CarShareAreaScreen(modifier: Modifier = Modifier) {
var allCarShareArea by remember { mutableStateOf<AllCarShareArea?>(null) }
val coroutineScope = rememberCoroutineScope()
LaunchedEffect(Unit){
coroutineScope.launch {
CarShareAreaApiManager.fetchCarShareArea()
.onSuccess { allCarShareArea = it }
.onFailure { Log.e("Api", "${it.message}") }
}
}
var context = LocalContext.current
Column (
modifier = modifier
.padding(25.dp)
.verticalScroll(rememberScrollState())
){
Text(
text = "Liste des Aires de Covoiturages"
)
allCarShareArea?.results?.forEach {
TextButton(
onClick = {detailsDialogCarShareArea(context, it)}
) {
ElevatedCard(
modifier = Modifier
.padding(10.dp, 5.dp)
.fillMaxSize(),
colors = CardColors(Color.DarkGray, Color.White, Color.DarkGray, Color.White)
) {
Row{
Column (
modifier = Modifier.weight(0.70f)
){
var modifierText = Modifier.padding(10.dp, 2.dp)
Text(text = "${it.name}", modifier = modifierText)
Text(text = "${it.town}", modifier = modifierText)
Text(text = "${it.places.let { it }?:"Non renseigné"}", modifier = modifierText)
}
ElevatedButton(
onClick = { TODO() },
modifier = Modifier.weight(0.30f)
.align(Alignment.CenterVertically)
.padding(10.dp, 0.dp)
) {
Image(
painter = painterResource(id = R.drawable.outline_map_24),
contentDescription = "Carte"
)
}
}
}
}
}
}
}
private fun detailsDialogCarShareArea(
context: Context,
carShareArea: CarShareArea
) {
val builder: AlertDialog.Builder = AlertDialog.Builder(context)
builder
.setTitle("Details")
.setMessage("${carShareArea.toString()}")
val dialog = builder.create()
dialog.show()
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MyApplicationTheme {
CarShareAreaScreen()
}
}
我需要解决这个错误,但不知道该怎么做;如果有人曾经遇到过这个错误和/或知道如何处理,我愿意采纳他的建议!
注:这是一个学生项目,因此如果有些地方不完美也很正常,通常它是可以跑通的
解决方案
这是Compose Multiplatform的一个已知问题:
https://youtrack.jetbrains.com/projects/CMP/issues/CMP-8617
在那里有人提出了建议...
请使用
org.jetbrains.compose.ui:ui-util(1.10.2) 包。
这很可能是JetBrains的一个分叉,不在 androidx.compose.ui.util 包中。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。