You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Compose Scaffold代码报错求助:mutableIntStateOf等相关错误

问题排查与解决方案

错误1:Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

出现这个错误是因为使用by委托语法时,缺少Compose状态委托必需的扩展函数导入。直接添加以下两个导入即可:

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

这两个包提供的getValue/setValue函数,是让mutableIntStateOf返回的State对象支持by委托的关键。

错误2:Unresolved reference: mutableIntStateOf

mutableIntStateOf是Compose状态管理API的一部分,你的代码里没导入对应的包,添加下面这行导入就能解决:

import androidx.compose.runtime.mutableIntStateOf

错误3:This material API is experimental and is likely to change or to be removed in the future

topAppBarColors属于Material3的实验性API,必须显式声明允许使用实验性功能,有两种处理方式:

方式1:给单个Composable加注解

在ScaffoldExample函数上方添加@OptIn(ExperimentalMaterial3Api::class)注解,同时导入注解包:

import androidx.compose.material3.ExperimentalMaterial3Api

修改后的函数头部:

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScaffoldExample() {
    // 原有代码不变
}

方式2:全局启用实验性API

如果不想每个用到实验API的地方都加注解,可以在模块级build.gradle(或build.gradle.kts)的android块里添加配置:

android {
    // 其他已有配置...
    composeOptions {
        kotlinCompilerExtensionVersion = "你的Compose版本号" // 比如"1.5.3",要和项目依赖版本一致
    }
    kotlinOptions {
        freeCompilerArgs += "-opt-in=androidx.compose.material3.ExperimentalMaterial3Api"
    }
}

完整修改后的代码

导入部分:

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme

函数部分:

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScaffoldExample() {
    var presses by remember { mutableIntStateOf(0) }

    Scaffold(
        topBar = {
            TopAppBar(
                colors = topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.primary,
                ),
                title = {
                    Text("Top app bar")
                }
            )
        },
        bottomBar = {
            BottomAppBar(
                containerColor = MaterialTheme.colorScheme.primaryContainer,
                contentColor = MaterialTheme.colorScheme.primary,
            ) {
                Text(
                    modifier = Modifier
                        .fillMaxWidth(),
                    textAlign = TextAlign.Center,
                    text = "Bottom app bar",
                )
            }
        },
        floatingActionButton = {
            FloatingActionButton(onClick = { presses++ }) {
                Icon(Icons.Default.Add, contentDescription = "Add")
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding),
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            Text(
                modifier = Modifier.padding(8.dp),
                text =
                """
                    This is an example of a scaffold. It uses the Scaffold composable's parameters to create a screen with a simple top app bar, bottom app bar, and floating action button.

                    It also contains some basic inner content, such as this text.

                    You have pressed the floating action button $presses times.
                """.trimIndent(),
            )
        }
    }
}

内容的提问来源于stack exchange,提问作者pcp pcp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:46:00