[英]Android Kotlin: Error when using 'suspend' in DAO - Following CodeLabs guide
[英]Error while using suspend with DAO methods
我收到如下所述的編譯錯誤。 我已經嘗試過針對類似問題提供的不同解決方案,但它們都不適合我。
請幫我解決一下這個。
我得到的錯誤: -
(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)
這是我在構建時遇到的編譯錯誤:-
DAO class:-
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>>
}
實體 Class
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
}
數據庫 Class
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
}
}
}
}
我的構建 gradle
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 中的所有方法中刪除掛起時,也沒有編譯錯誤。
您所有的房間版本都應該相同。 您已明確指出與 rest 不同的common
。 為什么不讓common
只是傳遞性的呢?
也有同樣的問題,將我的 room_version 從'2.3.0'
更新為'2.4.0-beta02'
修復了它!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.