【问题标题】:Android Jetpack Compose - java.lang.NoSuchMethodError: No virtual method setContent(Lkotlin/jvm/functions/Function0;)Android Jetpack Compose - java.lang.NoSuchMethodError: 没有虚拟方法 setContent(Lkotlin/jvm/functions/Function0;)
【发布时间】:2022-01-14 15:16:25
【问题描述】:

我在尝试运行 setContent{ Composable() } 时遇到 java.lang.NoSuchMethodError 异常。

完整代码:

class ComposeFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
        ComposeView(requireContext()).apply { setContent { Text("hello") } }

}

完全例外:

java.lang.NoSuchMethodError: No virtual method setContent(Lkotlin/jvm/functions/Function0;)V in class Landroidx/compose/ui/platform/ComposeView; or its super classes (declaration of 'androidx.compose.ui.platform.ComposeView' appears in /data/app/~~3OmVKUoYitZ_S4H81xmyuw==/my.app.package-PAQxAKmtRuhnzp4M2DME8w==/base.apk)

类似问题的解决方案/答案建议添加buildFeatures { compose = true } 或kotlinCompilerExtensionVersion,我已经这样做了,但问题仍然存在。

我完整的 compose Gradle 配置如下:

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
    useIR = true
}

buildFeatures {
    compose = true
}

composeOptions{
    kotlinCompilerExtensionVersion = "1.0.5"
}

implementation( "androidx.activity:activity-compose:1.3.1" )
implementation( "androidx.activity:activity-compose:1.3.1" )
implementation( "androidx.compose.material:material:1.0.5" )
implementation( "androidx.compose.animation:animation:1.0.5" )
implementation( "androidx.compose.ui:ui-tooling:1.0.5" )
implementation( "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07" )
androidTestImplementation( "androidx.compose.ui:ui-test-junit4:1.0.5" )

【问题讨论】:

  • 请提供一个示例项目来重现该错误。我尝试创建一个新项目(Fragment + ViewModel),添加你的 gradle 配置,用你的替换生成的片段代码,它运行良好。
  • 嗨@PhilipDukhov,我会在有机会时这样做,谢谢。

标签: android gradle android-jetpack-compose


【解决方案1】:

您的 Compose 版本非常旧,几乎可以肯定是您出现问题的原因。使用以下内容更新您的项目 gradle。确保也升级插件版本:

buildscript {
    ext {
        compose_version = '1.1.0-rc01'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0'
    }
}

确保所有依赖项都使用最新的稳定版本。

您还应该更新您的 Kotlin 插件版本:

211-1.6.10-release-923-AS7442.40

如果你不知道在哪里设置这个,点击:

Android Studio > Preferences > Languages & Frameworks > Kotlin

最后,我强烈建议您使用 Java 11 而不是 8,并将应用的 build.gradle 设置为:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

apply from: 'codeinc.gradle'

android {
    compileSdkVersion 31
    buildToolsVersion "31.0.0"

    defaultConfig {
        applicationId "mydomain.myapp.blablahblah"
        minSdkVersion 21
        targetSdkVersion 31
        versionName "1.0.0"
        
        versionCode 1
    }
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.6.0'
    }
}

【讨论】:

  • 虽然建议不错,但遗憾的是更新所有内容并没有解决问题。
  • “很老”?!在您撰写本文时,Compose 1.0.5(由提问者使用)还不到 3 个月大。
【解决方案2】:

今天遇到了同样的异常,所以我把我的解决方案放在这里以防其他人需要它。

就我而言,这是因为build.gradle 文件中缺少以下设置。

Version.COMPOSE 可能是您的撰写版本,在我的情况下是1.1.0-alpha05

【讨论】:

    【解决方案3】:
    @Composable
    private fun Greeting() {
        Text(
            text = stringResource(R.string.channel_id),
            style = MaterialTheme.typography.h5,
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 10.dp)
                .wrapContentWidth(Alignment.CenterHorizontally)
        )
    }
    
    class ShowAvatarImageActivity : AppCompatActivity() {
        ......
        val greeting = findViewById<ComposeView>(R.id.greeting)
        greeting.setContent {
            Greeting()
        }
        ......
    }
    

    Gradle Config 比如你

    我的xml文件是这样的

    <androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:background="#000">
    
        ......
    
      <androidx.compose.ui.platform.ComposeView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/greeting"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    我使用纯 Kotlin 代码来比较要测试的单独函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-28
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2019-11-20
      相关资源
      最近更新 更多