首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android数据库自动生成代码不编译

Android数据库自动生成代码不编译
EN

Stack Overflow用户
提问于 2022-07-11 21:38:55
回答 1查看 197关注 0票数 0

我无法编译项目,因为自动生成的代码没有编译.这是我的代码:

我在使用2.4.2版的“房间”

我的数据库类:

RecipeDatabase.java

代码语言:javascript
运行
复制
@Database(entities = {Person.class, Recipe.class}, version = 1)
public abstract class RecipeDatabase extends RoomDatabase {
    public abstract PersonDao personDao();
    public abstract RecipeDao recipeDao();
}

实体类:

Person.java

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
@Dao
public interface PersonDao {

    @Query("SELECT * FROM people")
    List<Person> getAll();

    @Query("SELECT name FROM people")
    List<String> getNames();
}

RecipeDao.java

代码语言:javascript
运行
复制
@Dao
public interface RecipeDao {

    @Query("SELECT * FROM recipes")
    List<Recipe> getAll();

    @Query("SELECT name FROM recipes")
    List<String> getNames();
}

下面是不编译的代码(在java (生成)下):

RecipeDatabase_Impl.java

代码语言:javascript
运行
复制
      @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

代码语言:javascript
运行
复制
@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)
        }

全部错误:

代码语言:javascript
运行
复制
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
代码语言:javascript
运行
复制
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

当然,编辑生成的文件中的任何内容都没有帮助,而且我在任何论坛上都找不到这样的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72944926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档