当我试图在TaskClass中创建一个实体时,它说它无法将优先级类型保存到数据库中,我遇到了这个错误。Priority是我用几个常量变量创建的枚举类。
我的错误
error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
public Priority priority;
^
我搞不清楚到底是什么问题。旁注:我正在看一个android开发课程,我只是跟着指导老师,但他做的同样的事情是给我一个错误,我怎么能修复这个问题?
我的优先枚举类
package com.bawp.todoister.model;
public enum Priority {
HIGH,
MEDIUM,
LOW
}
我的任务课
package com.bawp.todoister.model;
import androidx.room.ColumnInfo;
import androidx.room.Database;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.util.Date;
@Entity(tableName = "task_table")
public class Task {
@ColumnInfo(name = "task_id")
@PrimaryKey(autoGenerate = true)
public long taskId;
public String task;
@ColumnInfo(name = "priority")
public Priority priority;
@ColumnInfo(name = "due_date")
public Date dueDate;
@ColumnInfo(name = "date_created")
public Date dateCreated;
@ColumnInfo(name = "is_done")
public Boolean isDone;
public Task(String task, Priority priority, Date dueDate, Date dateCreated, Boolean isDone) {
this.task = task;
this.priority = priority;
this.dueDate = dueDate;
this.dateCreated = dateCreated;
this.isDone = isDone;
}
public long getTaskId() {
return taskId;
}
public void setTaskId(long taskId) {
this.taskId = taskId;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
public Priority getPriority() {
return priority;
}
public void setPriority(Priority priority) {
this.priority = priority;
}
public Date getDueDate() {
return dueDate;
}
public void setDueDate(Date dueDate) {
this.dueDate = dueDate;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Boolean getDone() {
return isDone;
}
public void setDone(Boolean done) {
isDone = done;
}
@Override
public String toString() {
return "Task{" +
"taskId=" + taskId +
", task='" + task + '\'' +
", priority=" + priority +
", dueDate=" + dueDate +
", dateCreated=" + dateCreated +
", isDone=" + isDone +
'}';
}
}
我的项目等级文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
appCompatVersion = '1.2.0'
constraintLayoutVersion = '2.0.2'
coreTestingVersion = '2.1.0'
lifecycleVersion = '2.2.0'
materialVersion = '1.2.1'
roomVersion = '2.2.5'
// testing
junitVersion = '4.13.1'
espressoVersion = '3.1.0'
androidxJunitVersion = '1.1.2'
}
我的模块GRADLE文件
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.bawp.todoister"
minSdkVersion 29
targetSdkVersion 30
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
}
}
dependencies {
implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
// 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-runtime:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-viewmodel:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
将房间版本从2.2.5升级到2.3.0。这就是添加了对枚举的内置支持的时候。
所以,当我试图编译它时,我得到了这个错误: 我不熟悉房间实施,所以我不知道如何解决这个问题。
我要用作的数据类如下: My details对象是一个名为的自定义类的列表,其中包含: 此列表中只有一项。正如上面的数据类中提到的,我还为实体中的特定字段指定了一个TypeConzer类。其定义如下: 为了完成我的问题,下面是我的实现: 采用这种方法,我在构建应用程序时会出现以下错误: 甚至我也怀疑出了问题,因为我的IDE没有显示任何地方都可以使用的中的函数。像这样: 我知道很多人过去也在Stac
文件CrimetypeConverters.kt 错误:错误:无法确定如何将此字段保存到数据库中。可以考虑为其添加一个类型转换器。private java.util.date日期;C:\users\asus\AndroidStudioProjects\CriminalIntentV2\app\build\tmp\kapt3\stubs\debug\com\bignerdranch\android\
当我想将我的JSON记录到房间数据库中时,我遇到了一个问题。 我的数据类是: )
我有以下型号: 然后告诉我这个错误:
这是第一次使用Room数据库,我想将数据保存到嵌套类中 植物种类: 这是植物类别类: 我试着用 但这些注释似乎不可用也许我缺少了一个库? 这是我的gradle文件: 我将感谢你的帮助!