由于接收器类型不匹配,以下候选者均不适用

use*_*654 3 android kotlin android-viewmodel

我已经尝试了我能找到的所有咒语,但在使用 viewModel 时仍然遇到问题。

这是有问题的代码:

class MainActivity: FlutterActivity(), OnSneakerClickListener, OnSneakerDismissListener {
private val httpclient = OkHttpClient()
val stripe = Stripe(this, "pk_test_yadayadayada")
private val viewModel: StripeIntentViewModel by viewModels() //<--dragons be here
Run Code Online (Sandbox Code Playgroud)

当我手动输入“import androidx.activity.viewModels”时,它会将其显示为“未使用的导入指令”。

我的模块 build.gradle 文件如下所示:

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'checkstyle'
id 'org.jetbrains.kotlin.plugin.parcelize'
 }
 ...
 compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}
...
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$androidLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$androidLifecycleVersion"
implementation "com.google.android.material:material:$materialVersion"
implementation "androidx.fragment:fragment-ktx:$fragmentVersion"
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

implementation "androidx.activity:activity-ktx:1.1.0"
implementation "androidx.fragment:fragment-ktx:1.2.5"
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

implementation "androidx.activity:activity-ktx:1.2.0-rc01"
Run Code Online (Sandbox Code Playgroud)

是不是少了点什么?

ian*_*ake 11

属性by viewModels()扩展是 的扩展androidx.activity.ComponentActivity,它提供了对 ViewModel 的支持(通过ViewModelStoreOwner接口)。

FlutterActivity根据其文档,仅扩展android.app.Activity且不扩展ComponentActivity。因此不可能by viewModels()从 a使用FlutterActivity

您可以根据此问题使用FlutterFragmentActivity, which extends FragmentActivity(其本身扩展) 。ComponentActivity