Android Room - 找不到符号 @androidx.room.TypeConverters(value = {StringDataConverter.class, ...})

bac*_*kup 5 android

你如何在 Android Room 中使用 List 我有一个表实体,我想通过 Android Room 将它保存在我的 SQLDatabase 中。我已经按照我可以在网上进行的所有操作进行了操作,并且没有 List 就可以了。但是当我在我的实体中添加列表项时,它停止工作:

Table.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})
Run Code Online (Sandbox Code Playgroud)

 

@Entity(tableName = "tables")
    data class Table(
        var name : String,
        var description : String,
        var image : String,
        var colNames : List<String> = emptyList(),
        var rows : List<Row> = emptyList()
    ){
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0

        override fun toString(): String {
            return "TableEntity(name='$name', description='$description', image='$image', id=$id)"
        }
    }
Run Code Online (Sandbox Code Playgroud)

 

data class Row(var items: List<Unit>)
Run Code Online (Sandbox Code Playgroud)

 

data class Unit(var value : Object, var type : String)
Run Code Online (Sandbox Code Playgroud)

我也试了一下,而不TypeConvertersTable同一个错误,但我得到了它的文件TablesDatabase

 

@Database(entities = [Table::class], version = 1)
@TypeConverters(StringDataConverter::class, RowDataConverter::class)
abstract class TablesDatabase : RoomDatabase(){
    abstract fun tableDao(): TableDao

    companion object {
        @Volatile private var instance: TablesDatabase? = null
        private val LOCK = Any()

        operator fun invoke(context: Context) = instance ?: synchronized(LOCK){
            instance ?: buildDataBase(context).also{
                instance = it
            }
        }    

        private fun buildDataBase(context: Context) =  Room.databaseBuilder(context.applicationContext, TablesDatabase::class.java, "tablesx2.db").build()
    }
}
Run Code Online (Sandbox Code Playgroud)

 

public class StringDataConverter {
    @TypeConverter
    public static List<String> fromString(String value) {
        Type listType = new TypeToken<List<String>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromList(List<String> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}

public class RowDataConverter {
    @TypeConverter
    public static List<Row> fromString(String value) {
        Type listType = new TypeToken<List<Row>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromList(List<Row> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ty Stackoverflow

编辑:我尝试使用和不使用将Table类注释为@TypeConverters(StringDataConverter::class, RowDataConverter::class)

编辑 2:我也尝试重写实体并使用 Java 而不是 Kotlin(仅用于实体文件)没有更改异常(附录 1

编辑3

我在build.gradle 中添加了以下几行(整个 Gradle-File附录 2

allprojects {
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xmaxerrs" << "5000"
            }
        }
    }

    android {
        defaultConfig {
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                            "room.schemaLocation":"$projectDir/schemas".toString(),
                            "room.incremental":"true",
                            "room.expandProjection":"true"]
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我看到了我已经看到的错误:

TablesDatabase.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})
  symbol: class StringDataConverter
Run Code Online (Sandbox Code Playgroud)

TablesDatabase.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})
  symbol: class RowDataConverter
Run Code Online (Sandbox Code Playgroud)

此外还有这个新的:

TablesDatabase.java:8: error: [RoomProcessor:MiscError] androidx.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract class TablesDatabase extends androidx.room.RoomDatabase {
Run Code Online (Sandbox Code Playgroud)

附录1

@Entity(tableName = "tables")
    public class Table {
        @PrimaryKey(autoGenerate = true)
        Integer id = 0;
        String name = "";
        String description = "";
        String image = "";
        List<String> colNames = new ArrayList<>();
        List<Row> rows = new ArrayList<>();

        public Table(String name, String description, String image, List<String> colNames, List<Row> rows) {
            this.name = name;
            this.description = description;
            this.image = image;
            this.colNames = colNames;
            this.rows = rows;
        }

        public Table(String name, String description, String image, List<String> colNames) {
            this.name = name;
            this.description = description;
            this.image = image;
            this.colNames = colNames;
        }

        public Table(String name, String description, String image) {
            this.name = name;
            this.description = description;
            this.image = image;
        }

        public Table() {}

        @Override
        public String toString() {
            return "Table{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", description='" + description + '\'' +
                    ", image='" + image + '\'' +
                    ", colNames=" + colNames +
                    ", rows=" + rows +
                    '}';
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public List<String> getColNames() {
            return colNames;
        }

        public void setColNames(List<String> colNames) {
            this.colNames = colNames;
        }

        public List<Row> getRows() {
            return rows;
        }

        public void setRows(List<Row> rows) {
            this.rows = rows;
        }
    }
Run Code Online (Sandbox Code Playgroud)

附录二

apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'

    allprojects {
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xmaxerrs" << "5000"
            }
        }
    }

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.2"
        defaultConfig {
            applicationId "de.hsos.ma.adhocdb"
            minSdkVersion 22
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true // This line

            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                            "room.schemaLocation":"$projectDir/schemas".toString(),
                            "room.incremental":"true",
                            "room.expandProjection":"true"]
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.core:core-ktx:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
        implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


        def cardViewVersion = "1.0.0"
        def recyclerViewVersion = "1.0.0"
        def glideVersion = "4.11.0"
        def roomVersion = "2.2.3"
        def materialIoVersion = "1.2.0-alpha04"
        def kotlinCoroutinesVersion = "1.3.3"
        def materialDialogVersion = "3.1.1"
        def gsonVersion = "2.8.6"

        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion"


        // room
        kapt "androidx.room:room-compiler:$roomVersion"
        implementation "androidx.room:room-runtime:$roomVersion"
        implementation "androidx.room:room-ktx:$roomVersion"

        //material io
        implementation "com.google.android.material:material:$materialIoVersion"
        // Card View
        implementation "androidx.cardview:cardview:$cardViewVersion"

        // Recyclerview
        implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"

        //glide
        implementation "com.github.bumptech.glide:glide:$glideVersion"
        annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"

        implementation "com.afollestad.material-dialogs:core:$materialDialogVersion"
        implementation "com.google.code.gson:gson:$gsonVersion"
    }
Run Code Online (Sandbox Code Playgroud)

Mir*_*iny 1

在表类中,您定义了colNames变量rows,就像List在 TypeConverters 类中使用ArrayList数据类型一样。因为List无法ArrayList自动转换,所以 Room 无法为这些变量找到合适的 TypeConverter,这就是错误的根源。要修复它,您必须在和类中都更改ArrayList为。ListStringDataConverterRowDataConverter

public class StringDataConverter {
    @TypeConverter
    public static List<String> fromString(String value) {
        Type listType = new TypeToken<ArrayList<String>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromArrayList(List<String> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}

public class RowDataConverter {
    @TypeConverter
    public static List<Row> fromString(String value) {
        Type listType = new TypeToken<ArrayList<Row>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromArrayList(List<Row> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}
Run Code Online (Sandbox Code Playgroud)



更新
请检查您是否正确导入StringDataConverterRowDataConverter分类。