Android Studio arctic fox 中的渲染问题

Render problem in Android Studio arctic fox

我尝试 运行 这段代码并在预览模式下查看它

package com.example.basiccodelab

import androidx.compose.ui.graphics.Color.Companion.Yellow
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Color.Companion.Yellow
import androidx.compose.ui.tooling.preview.Preview
import com.example.basiccodelab.ui.theme.BasicCodelabTheme

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            BasicCodelabTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Surface(color = Color.Yellow) {
        Text(text = "Hello $name!")
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    BasicCodelabTheme {
        Greeting("Android")
    }
}

但是出现这个问题

java.lang.NoSuchMethodError: 'void androidx.compose.ui.graphics.drawscope.DrawScope$DefaultImpls.drawRect-w2WG-Gw$default(androidx.compose.ui.graphics.drawscope.DrawScope, long, long, long, float, androidx.compose.ui.graphics.drawscope.DrawStyle, androidx.compose.ui.graphics.ColorFilter, androidx.compose.ui.graphics.BlendMode, int, java.lang.Object)'
    at androidx.compose.foundation.Background.drawRect(Background.kt:111)
    at androidx.compose.foundation.Background.draw(Background.kt:103)
    at androidx.compose.ui.node.ModifiedDrawNode.performDraw(ModifiedDrawNode.kt:102)
    at androidx.compose.ui.node.LayoutNodeWrapper$invoke.invoke(LayoutNodeWrapper.kt:260)
    at androidx.compose.ui.node.LayoutNodeWrapper$invoke.invoke(LayoutNodeWrapper.kt:259)
    at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:121)
    at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:75)
    at androidx.compose.ui.node.LayoutNodeWrapper.invoke(LayoutNodeWrapper.kt:259)
    at androidx.compose.ui.node.LayoutNodeWrapper.invoke(LayoutNodeWrapper.kt:53)
    at androidx.compose.ui.platform.RenderNodeLayer.drawLayer(RenderNodeLayer.android.kt:220)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:244)
    at androidx.compose.ui.node.ModifiedLayoutNode.performDraw(ModifiedLayoutNode.kt:82)
    at androidx.compose.ui.node.LayoutNodeWrapper$invoke.invoke(LayoutNodeWrapper.kt:260)
    at androidx.compose.ui.node.LayoutNodeWrapper$invoke.invoke(LayoutNodeWrapper.kt:259)
    at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:1788)
    at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:116)
    at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:75)
    at androidx.compose.ui.node.LayoutNodeWrapper.invoke(LayoutNodeWrapper.kt:259)
    at androidx.compose.ui.node.LayoutNodeWrapper.invoke(LayoutNodeWrapper.kt:53)
    at androidx.compose.ui.platform.RenderNodeLayer.drawLayer(RenderNodeLayer.android.kt:220)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:244)
    at androidx.compose.ui.node.LayoutNode.draw$ui_release(LayoutNode.kt:800)
    at androidx.compose.ui.node.InnerPlaceable.performDraw(InnerPlaceable.kt:116)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:249)
    at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.performDraw(DelegatingLayoutNodeWrapper.kt:67)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:249)
    at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.performDraw(DelegatingLayoutNodeWrapper.kt:67)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:249)
    at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.performDraw(DelegatingLayoutNodeWrapper.kt:67)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:249)
    at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.performDraw(DelegatingLayoutNodeWrapper.kt:67)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:249)
    at androidx.compose.ui.node.LayoutNode.draw$ui_release(LayoutNode.kt:800)
    at androidx.compose.ui.platform.AndroidComposeView.dispatchDraw(AndroidComposeView.android.kt:656)
    at android.view.View.draw_Original(View.java:22353)
    at android.view.View_Delegate.draw(View_Delegate.java:56)
    at android.view.View.draw(View.java:22323)
    at android.view.View.draw_Original(View.java:22223)
    at android.view.View_Delegate.draw(View_Delegate.java:68)
    at android.view.View.draw(View.java:21995)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.draw_Original(View.java:22221)
    at android.view.View_Delegate.draw(View_Delegate.java:68)
    at android.view.View.draw(View.java:21995)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at androidx.compose.ui.tooling.preview.ComposeViewAdapter.dispatchDraw(ComposeViewAdapter.kt:444)
    at android.view.View.draw_Original(View.java:22353)
    at android.view.View_Delegate.draw(View_Delegate.java:56)
    at android.view.View.draw(View.java:22323)
    at android.view.View.draw_Original(View.java:22223)
    at android.view.View_Delegate.draw(View_Delegate.java:68)
    at android.view.View.draw(View.java:21995)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.draw_Original(View.java:22221)
    at android.view.View_Delegate.draw(View_Delegate.java:68)
    at android.view.View.draw(View.java:21995)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.draw_Original(View.java:22221)
    at android.view.View_Delegate.draw(View_Delegate.java:68)
    at android.view.View.draw(View.java:21995)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.draw_Original(View.java:22353)
    at android.view.View_Delegate.draw(View_Delegate.java:56)
    at android.view.View.draw(View.java:22323)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.renderAndBuildResult(RenderSessionImpl.java:553)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:449)
    at com.android.layoutlib.bridge.BridgeRenderSession.render(BridgeRenderSession.java:120)
    at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:137)
    at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:119)
    at com.android.tools.idea.rendering.RenderTask.lambda$renderInner(RenderTask.java:1019)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:187)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

在你的 build.gradle 中,尝试将 Kotlin 更新到 1.5.10 并将 Compose 更新到 beta08。

像这样:

    buildscript {
    ext {
        compose_version = '1.0.0-beta08'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.1.0-alpha01"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

确保“androidx.activity:activity-compose:...”具有与您的 compose_version

相同的版本