我无法编译项目,因为自动生成的代码没有编译.这是我的代码:
我在使用2.4.2版的“房间”
我的数据库类:
RecipeDatabase.java
@Database(entities = {Person.class, Recipe.class}, version = 1)
public abstract class RecipeDatabase extends RoomDatabase {
public abstract PersonDao personDao();
public abstract RecipeDao recipeDao();
}
实体类:
Person.java
@Entity(tableName = "people")
public class Person {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "person_id")
public int id;
public String name;
public Person(String name) {
this.name = name;
}
}
Recipe.java
@Entity(tableName = "recipes")
public class Recipe {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "recipe_id")
public int id;
public String name;
public FoodType type; //it is an enum
public Recipe(String name, FoodType type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public String getTypeName() {
return type.getName();
}
public int getImageResource() {
return type.getImgResource();
}
}
DAO类:
PersonDao.java
@Dao
public interface PersonDao {
@Query("SELECT * FROM people")
List<Person> getAll();
@Query("SELECT name FROM people")
List<String> getNames();
}
RecipeDao.java
@Dao
public interface RecipeDao {
@Query("SELECT * FROM recipes")
List<Recipe> getAll();
@Query("SELECT name FROM recipes")
List<String> getNames();
}
下面是不编译的代码(在java (生成)下):
RecipeDatabase_Impl.java
@Override
protected void onCreate(SupportSQLiteDatabase _db) {
if (mCallbacks != null) {
for (int _i = 0, _size = mCallbacks.size(); _i < _size; _i++) {
mCallbacks.get(_i).onCreate(_db);
}
}
}
...
@Override
protected RoomOpenHelper.ValidationResult onValidateSchema(SupportSQLiteDatabase _db) {
final HashMap<String, TableInfo.Column> _columnsPeople = new HashMap<String, TableInfo.Column>(2);
_columnsPeople.put("person_id", new TableInfo.Column("person_id", "INTEGER", true, 1, null, TableInfo.CREATED_FROM_ENTITY));
_columnsPeople.put("name", new TableInfo.Column("name", "TEXT", false, 0, null, TableInfo.CREATED_FROM_ENTITY));
final HashSet<TableInfo.ForeignKey> _foreignKeysPeople = new HashSet<TableInfo.ForeignKey>(0);
final HashSet<TableInfo.Index> _indicesPeople = new HashSet<TableInfo.Index>(0);
final TableInfo _infoPeople = new TableInfo("people", _columnsPeople, _foreignKeysPeople, _indicesPeople);
final TableInfo _existingPeople = TableInfo.read(_db, "people");
if (! _infoPeople.equals(_existingPeople)) {
return new RoomOpenHelper.ValidationResult(false, "people(com.szabolcst.recipes.model.Person).\n"
+ " Expected:\n" + _infoPeople + "\n"
+ " Found:\n" + _existingPeople);
}
final HashMap<String, TableInfo.Column> _columnsRecipes = new HashMap<String, TableInfo.Column>(3);
_columnsRecipes.put("recipe_id", new TableInfo.Column("recipe_id", "INTEGER", true, 1, null, TableInfo.CREATED_FROM_ENTITY));
_columnsRecipes.put("name", new TableInfo.Column("name", "TEXT", false, 0, null, TableInfo.CREATED_FROM_ENTITY));
_columnsRecipes.put("type", new TableInfo.Column("type", "TEXT", false, 0, null, TableInfo.CREATED_FROM_ENTITY));
final HashSet<TableInfo.ForeignKey> _foreignKeysRecipes = new HashSet<TableInfo.ForeignKey>(0);
final HashSet<TableInfo.Index> _indicesRecipes = new HashSet<TableInfo.Index>(0);
final TableInfo _infoRecipes = new TableInfo("recipes", _columnsRecipes, _foreignKeysRecipes, _indicesRecipes);
final TableInfo _existingRecipes = TableInfo.read(_db, "recipes");
if (! _infoRecipes.equals(_existingRecipes)) {
return new RoomOpenHelper.ValidationResult(false, "recipes(com.szabolcst.recipes.model.Recipe).\n"
+ " Expected:\n" + _infoRecipes + "\n"
+ " Found:\n" + _existingRecipes);
}
return new RoomOpenHelper.ValidationResult(true, null);
}
}, "1c493a2b22e38e23def00f0336257ee2", "6829c4aff16c254527acbdc5d6cecc85");
final SupportSQLiteOpenHelper.Configuration _sqliteConfig = SupportSQLiteOpenHelper.Configuration.builder(configuration.context)
.name(configuration.name)
.callback(_openCallback)
.build();
final SupportSQLiteOpenHelper _helper = configuration.sqliteOpenHelperFactory.create(_sqliteConfig);
return _helper;
}
在这两种方法中,问题在于访问修饰符(受保护),错误是:'attempting to assign weaker access privileges; was public
‘
这些重写的方法是:
RoomOpenHelper.kt
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
abstract class Delegate(@JvmField val version: Int) {
abstract fun dropAllTables(database: SupportSQLiteDatabase)
abstract fun createAllTables(database: SupportSQLiteDatabase)
abstract fun onOpen(database: SupportSQLiteDatabase)
abstract fun onCreate(database: SupportSQLiteDatabase)
...
@Suppress("DEPRECATION")
open fun onValidateSchema(db: SupportSQLiteDatabase): ValidationResult {
validateMigration(db)
return ValidationResult(true, null)
}
全部错误:
error: onCreate(SupportSQLiteDatabase) in <anonymous com.szabolcst.recipes.persistance.RecipeDatabase_Impl$1> cannot override onCreate(SupportSQLiteDatabase) in Delegate
protected void onCreate(SupportSQLiteDatabase _db) {
^
attempting to assign weaker access privileges; was public
error: onValidateSchema(SupportSQLiteDatabase) in <anonymous com.szabolcst.recipes.persistance.RecipeDatabase_Impl$1> cannot override onValidateSchema(SupportSQLiteDatabase) in Delegate
protected RoomOpenHelper.ValidationResult onValidateSchema(SupportSQLiteDatabase _db) {
^
attempting to assign weaker access privileges; was public
当然,编辑生成的文件中的任何内容都没有帮助,而且我在任何论坛上都找不到这样的东西。
发布于 2022-07-12 13:36:57
将implementation "androidx.room:room-runtime:2.4.2"
改为implementation "androidx.room:room-runtime:2.3.0"
似乎很有帮助,因为现在使用的是RoomOpenHelper.java而不是kotlin版本,而在java版本中,访问修饰符是protected
。
https://stackoverflow.com/questions/72944926
复制相似问题