gpt4 book ai didi

android - 将挂起与 DAO 方法一起使用时出错

转载 作者:行者123 更新时间:2023-12-05 00:02:19 24 4
gpt4 key购买 nike

我收到如下所述的编译错误。我已经尝试过针对类似问题提供的不同解决方案,但它们都不适合我。
请帮我解决一下这个。
我得到的错误: -
(1)

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
(2)
error: Not sure how to handle insert method's return type.
public abstract java.lang.Object addTransaction(@org.jetbrains.annotations.NotNull()
(3)
error: Not sure how to handle delete method's return type. Currently the supported 
return types are void, int or Int.
public abstract java.lang.Object deleteTransaction(@org.jetbrains.annotations.NotNull()
(4)
error: Type of the parameter must be a class annotated with @Entity or a 
collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
(5)
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)
这是我在构建时遇到的编译错误:-
enter image description here
DAO 类:-
package com.tallileo.myapplication.TransactionDatabase

import androidx.lifecycle.LiveData
import androidx.room.*

@Dao
interface TransactionDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addTransaction(transaction:Transaction)

@Delete
suspend fun deleteTransaction(transaction:Transaction)

@Query("SELECT * FROM transactions ORDER BY id DESC")
fun getAllTransaction():LiveData<List<Transaction>>
}
实体类
package com.tallileo.myapplication.TransactionDatabase

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "transactions")
data class Transaction(
val transactionType:String,
val amountType:String,
val name:String,
val amount:String,
val accountTo:String,
val accountFrom:String,
val categoryType:String,
val currency:String,
var additionalNote:String=""
) {
@PrimaryKey(autoGenerate = true)
var id:Int=0
}
数据库类
package com.tallileo.myapplication.TransactionDatabase

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Transaction::class], version = 1, exportSchema = true)
abstract class TransactionDatabase : RoomDatabase() {

abstract fun transactDao():TransactionDao

companion object {
// Singleton prevents multiple instances of database opening at the
// same time.
@Volatile
private var INSTANCE: TransactionDatabase? = null

fun getDatabase(context: Context): TransactionDatabase {
// if the INSTANCE is not null, then return it,
// if it is, then create the database
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
TransactionDatabase::class.java,
"TransactionDatabase"
).build()
INSTANCE = instance
// return instance
instance
}
}
}
}
我的构建等级
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.tallileo.myapplication"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled 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 {
def roomVersion = "2.3.0"

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

//Lottie Animation
implementation 'com.airbnb.android:lottie:3.4.0'

//room components
implementation("androidx.room:room-runtime:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")
implementation 'androidx.room:room-common:2.3.0'
implementation("androidx.room:room-ktx:$roomVersion")
}
我的 kotlin 版本是 1.6.0。此外,当我从 DAO 中的所有方法中删除挂起时,也没有编译错误。

最佳答案

您所有的房间版本都应该相同。您已明确调用 common与其他人不同。为什么不让common只是进来传递?

关于android - 将挂起与 DAO 方法一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70229119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com