安卓房间 : Views via @DatabaseView tag not generated

标签 android android-room androidx

这是我的代码。我已经按照说明做了所有的事情。通过 @DATABASEVIEW(value=?, viewName=?) 创建 View 标记 标记 Room 数据库类中的 View 但是当我查询 select against VIEWS 在构建过程中抛出异常时: 查询有问题:[SQLITE_ERROR] SQL 错误或缺少数据库(没有这样的表:USER_RATE_INFO_VIEW 我探索了自动生成的数据库实现类,但没有找到创建 View 的脚本。 enter image description here

@Database(entities = {BankEntity.class, BankRateEntity.class, CommentEntity.class, ExchangeOfficeEntity.class,
        FBankEntity.class, FBankRateEntity.class, FRateEntity.class, RateByUserEntity.class,
        RateInfoEntity.class, UserEntity.class, UserMessageEntity.class, UserRateEntity.class},
        views = {BankRateView.class, ChatView.class, FBankRateView.class, RateInfoView.class, UserRateVew.class},
        version = 9)
public abstract class RoomDB extends RoomDatabase {
    private static RoomDB INSTANCE;

    public abstract RoomDao roomDao();

    public static RoomDB getDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), RoomDB.class, "database.db")
                            // allow queries on the main thread.
                            // Don't do this on a real app! See PersistenceBasicSample for an example.
                            .allowMainThreadQueries()
                            .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9)
                            .build();
        }
        return INSTANCE;
    }
}

@Getter
@Setter
@DatabaseView(value = TUserRate.VIEW_SQL_CMD, viewName = TUserRate.VIEW_NAME)
public class UserRateVew extends UserRateEntity{

    @ColumnInfo(name = TUserRate.COLUMN_USER_RATING)
    private float userRating;

}

最佳答案

我有同样的错误,只是解决它。 (我用的是kotlin,所以你需要在java中做相应的改动)

1.检查你的build.gradle依赖(有两个,一个显示在app文件夹的“Android”中,另一个显示在项目根文件夹的“Project”中)

project root / build.gradle I add as folllow

ext.ktlintVersion = '0.30.0' in buildscript{}
plugins {
    id "com.diffplug.gradle.spotless" version "3.13.0"
}
spotless {
kotlin {
    target "**/*.kt"
    ktlint(ktlintVersion)
    }
}

app / build.gradle

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
} in android{}

following dependency seems must at top
kapt "androidx.room:room-compiler:2.1.0-beta01"
implementation 'androidx.core:core-ktx:1.0.2'

this one just below previous mention denpendency
implementation 'androidx.room:room-runtime:2.1.0-beta01'
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0-alpha01"

following at bottom but before androidTestImplementation; kotlin_version should be 1.3.31
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"

<强>2。检查使用@databaseview 的 DAO 实现,尤其是返回类型 必须匹配查询查看结果列

当我更改 build.gradle 时,错误变成了导致数据库 View 无法生成的实际错误。

<强>3。使缓存无效/重启android studio

我花了几个小时来调试和解决这个错误,希望它能帮助到你。

(我不是来自英语国家,所以享受我的英语语法错误。哈哈哈)

关于安卓房间 : Views via @DatabaseView tag not generated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53894062/

相关文章:

android - 在 Phonegap 的 inappbrowser 中禁用缩放按钮

c# - 如何在 Xamarin.Forms 中自定义 TabbedPage 的选项卡项?

java - 如何从 MVVM 架构中的 Web 服务存储库类与 Activity/Fragment 进行通信

androidx Activity 无法转换为 AccountAuthenticatorActivity

android - Material Design NavigationView 还不兼容 AndroidX 吗?

java - Android/Java - 通过在分配之前检测剩余的可用 RAM 来防止 OutOfMemoryError

android - 不能声明占位符 View

android - 房间 DB : java. lang.reflect.InvocationTargetException

androidx 使用 SQLiteDiskIOException 使应用程序崩溃

android - 错误: duplicate value for resource 'attr/orientation' with config '' . aapt.v2.Aapt2Exception:Android资源编译失败