【问题标题】:error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. Android Studio Java错误:无法弄清楚如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。安卓工作室 Java
【发布时间】:2021-11-04 14:11:06
【问题描述】:

当我尝试在我的 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 +
                '}';
    }
}

我的项目 GRADLE 文件

// 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'
}

【问题讨论】:

    标签: java android database android-studio android-room


    【解决方案1】:

    将您的 Room 版本从 2.2.5 升级到 2.3.0。 That is when built-in support for enums was added.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-27
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 2019-05-17
      • 2019-10-04
      相关资源
      最近更新 更多