gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:28:22 30 4
gpt4 key购买 nike

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

Table.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})
@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)"
}
}
data class Row(var items: List<Unit>)
data class Unit(var value : Object, var type : String)

我也试过没有 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()
}
}
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;
}
}

泰堆栈溢出

编辑 : 我在有和没有注释 Table 的情况下都试过了。分类为 @TypeConverters(StringDataConverter::class, RowDataConverter::class)
编辑 2 :我还尝试重写实体并使用 Java 而不是 Kotlin(仅用于实体文件)没有更改异常( 附录 1 )

编辑 3 :

我在我的 中添加了以下行build.gradle (整个 Gradle 文件 附录 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"]
}
}
}
}

现在我看到了我已经看到的错误:
TablesDatabase.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})
symbol: class StringDataConverter


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

除了这个新的:
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 {

附录一
@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;
}
}

附录二
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"
}

最佳答案

在您的表类中,您定义了 colNamesrows变量为 List在你的 TypeConverters 类中你使用了 ArrayList数据类型。因为List无法转换为 ArrayList自动,然后 Room 无法为这些变量找到合适的 TypeConverter,这就是错误的来源。要修复它,您必须更改 ArrayListList在这两个 StringDataConverterRowDataConverter类。

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;
}
}

更新
请检查您是否导入了 StringDataConverterRowDataConverter正确上课。

关于Android Room - 找不到符号 @androidx.room.TypeConverters(value = {StringDataConverter.class, ...}),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59902066/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com