我正在尝试实现房间数据库,我已经完成了Official Website和AppDatabase的步骤。java文件如下:
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public static AppDatabase instance;
public static synchronized AppDatabase getInstance(Context context){
if (instance==null){
instance = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, "app_database").fallbackToDestructiveMigration().build();
}
return instance;
}
}
以及我用于房间的依赖项:
// Room Database
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava2 support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - RxJava3 support for Room
implementation "androidx.room:room-rxjava3:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
// optional - Paging 3 Integration
implementation "androidx.room:room-paging:2.5.0-alpha02"
// Room Database
它在此处返回2个错误:
onCreate(SupportSQLiteDatabase) in <anonymous com.example.testdb1.room.AppDatabase_Impl$1> cannot override onCreate(SupportSQLiteDatabase) in Delegate
attempting to assign weaker access privileges; was public
onValidateSchema(SupportSQLiteDatabase) in <anonymous com.example.testdb1.room.AppDatabase_Impl$1> cannot override onValidateSchema(SupportSQLiteDatabase) in Delegate
attempting to assign weaker access privileges; was public
它在“Chipmunk”版本之前工作(在“Bumblebee”中工作),但它开始抛出这些错误。
这是怎么回事?
6条答案
按热度按时间rkttyhzu1#
要修复Jetpack Compose和Paging 3的此错误,您只需要使用此库
qhhrdooz2#
通过Никандрова Елизавета's answer,我发现问题的根源是我从官方网站添加的可选实现之一。
这些依赖关系足以运行我的代码:
bfrts1fy3#
嗯,我刚刚也遇到了这个问题,看来你是在抄袭官方指南中的代码。这似乎是“androidx.room:room paging:2.5.0-alpha02”的问题,因此解决方案是将其替换为最新的稳定版本(当前为2.4.2),或者只使用变量
room_version
。您可以在https://androidx.tech/artifacts/room/room-paging/上检查最新版本。因此,您只需将其替换成以下代码即可解决此问题5m1hhzi44#
我试着逐一评论每一个依赖;每次逐个更改版本。
我突然想到:
上述注解负责生成以下异常:
导致原因:java.lang.RuntimeException:找不到com.safehaven.data.di.AppDatabase的实现。AppDatabase_Impl不存在
因此,这个被注解的库负责生成此错误出现的文件
AppDatabase_Impl
。所以现在这个图书馆要处理了。
其次是
RoomOpenHelper
中使用的Delegate
类具有以下代码:
和
显然两者都是公共的,而在用
AppDatabase_Impl
类中的抽象类示例化的SupportSQLiteOpenHelper.Callback _openCallback
中,使用受保护的访问修饰符公开这些方法,这会降低被重写方法的可见性。因此导致错误。
这应该是固定的。令人惊讶的是,我一周前就犯了这个错误。它是通过使用一个答案(在这里,在同一个问题中)作为方法(我不知道,它是如何做到的)来解决的,它再次浮出水面,现在不会消失。
出于这个原因,我至少建议自己不要使用房间数据库。我将像以前一样移动到ORMLite或SQLite数据库。
ftf50wuq5#
我有同样的错误,我可以在build.gradle(app)中解决以下问题。
在“插件”块中,添加了以下代码;
在“依赖关系”块中,添加了以下代码;
u5i3ibmn6#
我删除了密码-
换成这个-