为什么我的房间在科特林不能正常工作



我在kotlin用compose开发字典应用程序已经有一段时间了。我希望用户能够创建自己的字典作为应用程序的一个功能。我使用房间数据库,我已经完成了所有的集成,但我不明白为什么它总是出错。我检查了所有的东西,但找不到问题的根源。我将分享我的代码和错误。

错误

error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - kotlin.Unit
C:UsersenestAndroidStudioProjectsDictionaryAppappbuildtmpkapt3stubsdebugcomenestiglidictionaryappdatalocaleOwnDicDao.java:11: error: Not sure how to convert a Cursor to this method's return type (kotlin.Unit).
public abstract java.lang.Object GetAllDictionary(@org.jetbrains.annotations.NotNull()
^
C:UsersenestAndroidStudioProjectsDictionaryAppappbuildtmpkapt3stubsdebugcomenestiglidictionaryappdatalocaleOwnDicDao.java:11: warning: The query returns some columns [dicName, creationTime, uid] which are not used by kotlin.Unit. You can use @ColumnInfo annotation on the fields to specify the mapping. You can annotate the method with @RewriteQueriesToDropUnusedColumns to direct Room to rewrite your query to avoid fetching unused columns.  You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: dicName, creationTime, uid. Fields in kotlin.Unit: .
public abstract java.lang.Object GetAllDictionary(@org.jetbrains.annotations.NotNull()
^
C:UsersenestAndroidStudioProjectsDictionaryAppappbuildtmpkapt3stubsdebugcomenestiglidictionaryappdatalocaleOwnDicDao.java:33: error: Unused parameter: dicId
public abstract java.lang.Object Update(@org.jetbrains.annotations.Nullable()
^
C:UsersenestAndroidStudioProjectsDictionaryAppappbuildtmpkapt3stubsdebugcomenestiglidictionaryappdatalocaleOwnDictionaryDatabase.java:7: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
public abstract class OwnDictionaryDatabase extends androidx.room.RoomDatabase {
FAILURE: Build failed with an exception.
* What went wrong:
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)
Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin'
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
Caused by: java.lang.reflect.InvocationTargetException
at org.jetbrains.kotlin.gradle.internal.KaptExecution.run(KaptWithoutKotlincTask.kt:288)
at org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction.execute(KaptWithoutKotlincTask.kt:243)
Caused by: org.jetbrains.kotlin.kapt3.base.util.KaptBaseError: Error while annotation processing
at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:130)
at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:31)
at org.jetbrains.kotlin.kapt3.base.Kapt.kapt(Kapt.kt:45)
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

OwnDicDao

@Dao
interface OwnDicDao {

@Query("SELECT * FROM OwnDictionaries")
suspend fun GetAllDictionary()
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun Insert(dictionary: OwnDicEntity)
@Delete
suspend fun Delete(dictionary: OwnDicEntity)
@Query("DELETE FROM owndictionaries")
suspend fun DeleteAll()
@Query("UPDATE OwnDictionaries SET dicName =:dicName")
suspend fun Update(dicId:Int?,dicName:String)

}

当我注释掉@Query注释时,也就是说,当我注释完UpdateGetAllDictionary函数时,问题消失了,应用程序正常工作。我注意到了。

例如

/*  @Query("UPDATE OwnDictionaries SET dicName =:dicName")
suspend fun Update(dicId:Int?,dicName:String)*/
/* @Query("SELECT * FROM OwnDictionaries")
suspend fun GetAllDictionary()*/

OwnDicEntity

@Entity(tableName = "OwnDictionaries")
data class OwnDicEntity(
@ColumnInfo(name = "dicName") val ownDicName:String,
@ColumnInfo(name = "creationTime") val creationTime:String,
@PrimaryKey(autoGenerate = true) val uid: Int? = null
)

OwnDicDatabase

@Database(
entities = [OwnDicEntity::class],
version = 1
)
abstract class OwnDicDatabase: RoomDatabase() {
abstract val ownDicDao: OwnDicDao
}

build.gradle(模块(

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id  'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'org.jetbrains.kotlin.android.extensions'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.enestigli.dictionaryapp"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}

}
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 {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
androidExtensions {
experimental = true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
//implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'
// Coroutine Lifecycle Scopes
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
// Room
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"

// To use Kotlin annotation processing tool (kapt)

// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:2.3.0"

//Material theme3
implementation "androidx.compose.material3:material3:1.0.0-alpha12"
implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha12"
//skrape{it}
implementation 'org.jsoup:jsoup:1.12.1'
//compose
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

}

build.gradle(项目(

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
compose_version = '1.0.1'
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

当我完全删除房间时,例如,当我删除dao、数据库、实体等类时,它的工作原理和以前一样,正如我上面所说,当我移除或注释@Query查询时,它会工作。可能是什么问题,我已经处理这些错误两天了。我在以前的项目中使用room时从未遇到过这样的错误。

由于您的请求返回一些数据,因此您需要实现返回类型以挂起以下功能

@Dao
interface OwnDicDao {

@Query("SELECT * FROM OwnDictionaries")
suspend fun GetAllDictionary():List<OwnDicEntity> // here change
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun Insert(dictionary: OwnDicEntity)
@Delete
suspend fun Delete(dictionary: OwnDicEntity)
@Query("DELETE FROM owndictionaries")
suspend fun DeleteAll() // here no change need because it returns nothing
@Query("UPDATE OwnDictionaries SET dicName =:dicName")
suspend fun Update(dicId:Int?,dicName:String):List<OwnDicEntity> //here change

}

最新更新