解決JetpackCompose 编译通过但是运行报No static method setContent的问题

问题描述

报错信息

java.lang.NoSuchMethodError: No static method setContent$default(Landroidx/activity
/ComponentActivity;Landroidx/compose/runtime/CompositionContext;Lkotlin/jvm/functions
/Function0;ILjava/lang/Object;)V in class Landroidx/activity/compose/ComponentActivityKt; 
or its super classes (declaration of 'androidx.activity.compose.ComponentActivityKt'
 appears in /data/app/com.otter.player-Lx0SJMeWfCnOQZyMcc4S4w==/base.apk)at \
 com.otter.player.MainActivity.onCreate(MainActivity.kt:18)

出现的这个问题比较恶心,编译啥的都能通过,也能安装成功,但是就是运行无法成功,启动就闪退。

问题解决

在网上有人说在app下的build.gradle中的android{}下添加如下的代码

buildFeatures {
        compose = true
    }

这样又会引起一个错误,版本兼容的错误:如下所示

报错的信息大致为:

This version (1.0.5) of the Compose Compiler requires Kotlin version 1.5.31 but you appear to
  be using Kotlin version 1.7.10 which is not known to be compatible.  Please fix your 
  configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn 
  you!).

要完全解决这个问题,还需要在android{}添加如下代码:

composeOptions {
        kotlinCompilerExtensionVersion = "1.3.0" 
    }

然后就可以正常的运行了。由于Android由Java向Kotlin过渡,所以存在很多的版本兼容的问题,这些问题让开发者比较头疼,所以遇到问题一定要多上网查查。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值