gradle 错误参数的类型必须是带@Entity注解的类或其集合/数组

35g0bw71  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(143)

我有一个问题,我无法构建项目,因为以下错误,当我将suspend关键字添加到数据访问对象中的addNumber方法时发生:

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> $completion);
                                                    ^

我的道:

typealias NumberListLiveData = LiveData<List<Number>>

@Dao
interface NumbersDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun addNumber(number: Number)

    @Query("SELECT * FROM numbers_table")
    fun getAllNumbers(): NumberListLiveData

}

我的实体:

@Entity(tableName = "numbers_table")
data class Number(
    @PrimaryKey(true)
    val id: Int,
    @ColumnInfo(name = "value")
    val value: Int
)

Build.gradle.ktx:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("kotlin-kapt")
}

android {
    namespace = "com.ilya.room"
    compileSdk = 33

    defaultConfig {
        applicationId = "com.ilya.room"
        minSdk = 26
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"
    
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), 
"proguard-rules.pro")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        viewBinding = true
    }
}

dependencies {
    val roomVersion = "2.5.2"
    
    // Coroutines 
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")

    // Room
    implementation("androidx.room:room-runtime:$roomVersion")
    kapt("androidx.room:room-compiler:$roomVersion")
    implementation("androidx.room:room-ktx:$roomVersion")

    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.9.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

先谢谢你。

91zkwejq

91zkwejq1#

    • Kotlin**Number中有一个类型与您定义的类型冲突。

要验证这一点,请使用完整的软件包名称指定类型,例如。com.example.app.model.Number

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addNumber(number: com.example.app.model.Number)

如果这样做可以修复它,只要确保导入它,这样它就会掩盖Kotlin类型。

import com.example.app.model.Number

相关问题