簡體   English   中英

ROOM Dao ERROR:不確定如何處理插入方法的返回類型和不確定如何處理刪除方法的返回類型

[英]ROOM Dao ERROR: Not Sure How to handle insert method's return type & Not Sure how to handle delete method's return type

我正在給課程提供我創建的任何內容,以及錯誤顯示的位置。 我無法理解錯誤。

    package com.example.grocerylist
    
    import androidx.lifecycle.LiveData
    import androidx.room.*
    
    @Dao
    interface GroceryDao {
    
    
        @Insert(onConflict = OnConflictStrategy.REPLACE)
        suspend fun insert(item: GroceryItems)
    
        @Delete
        suspend fun delete(item: GroceryItems)
    
        @Query("SELECT * FROM groceryItems")
        fun getAllGroceryItems(): LiveData<List<GroceryItems>>
    
    }
    
    

實體

    import androidx.room.ColumnInfo
    import androidx.room.Entity
    import androidx.room.PrimaryKey
    
    
    @Entity(tableName = "groceryItems")
    data class GroceryItems(
        @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "ItemName") val ItemName:String,
        @ColumnInfo(name = "ItemQuantity") val ItemQuantity:Int,
        @ColumnInfo(name = "ItemPrice") val ItemPrice:Double,
    
    )

錯誤

我的 gradle 構建失敗,錯誤表示“dao” class 和錯誤:

1. areC:\Users\user\AndroidStudioProjects\GroceryList\app\build\tmp\kapt3\stubs\debug\com\example\grocerylist\GroceryDao.java:15: error: Not sure how to handle insert method's return type.
public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull()

2.C:\Users\user\AndroidStudioProjects\GroceryList\app\build\tmp\kapt3\stubs\debug\com\example\grocerylist\GroceryDao.java:17: 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);
                                                        ^
                                     ^
 
3.C:\Users\user\AndroidStudioProjects\GroceryList\app\build\tmp\kapt3\stubs\debug\com\example\grocerylist\GroceryDao.java:21: 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 delete(@org.jetbrains.annotations.NotNull()
                                     ^
4.C:\Users\user\AndroidStudioProjects\GroceryList\app\build\tmp\kapt3\stubs\debug\com\example\grocerylist\GroceryDao.java:23: 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);
                                                       ^

我錯過了什么,現在應該做什么請幫忙。

我不知道問題出在哪里,我按照開發人員官方文檔的所有說明來創建房間。

依賴項

依賴{

 implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"

    // Dependencies for working with Architecture components
    // You'll probably have to update the version numbers in build.gradle (Project)

    // Room components
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"
  
    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

    // UI
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"

    // Testing
    testImplementation "junit:junit:$rootProject.junitVersion"
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"
}

                                              

嘗試向應用程序的build.gradle文件添加依賴項:

implementation "androidx.room:room-coroutines:${rootProject.roomVersion}"

然后清理、同步和構建項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM