gpt4 book ai didi

java - 在 kotlin 中首次构建 Room 数据库时如何修复异常

转载 作者:行者123 更新时间:2023-11-29 18:48:25 27 4
gpt4 key购买 nike

我在第一次创建房间数据库时一直遇到这个异常

  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Package.getName()' on a null object reference

这是我的房间数据库代码:

@Database(entities = [Channel::class], version = 1)
abstract class ChannelRoomDatabase : RoomDatabase() {

abstract fun channelDao(): ChannelDao

companion object {
private var INSTANCE: ChannelRoomDatabase? = null

fun getInstance(application: Application): ChannelRoomDatabase? {
if (INSTANCE == null) {

INSTANCE = Room.databaseBuilder(application,
ChannelRoomDatabase::class.java,
"channels")
.build()
}

return INSTANCE
}
}

}

还有gradle代码:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

def room_version = "1.1.1"

implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"

我在构建项目时也遇到了这个错误:

Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.

Folder /home/Desktop/MySendBird/app/build/generated/source/kaptKotlin/debug


Folder /home/Desktop/MySendBird/app/build/generated/source/kaptKotlin/release


3rd-party Gradle plug-ins may be the cause

可能是什么原因?我在这里看到过类似的问题,但没有任何线索。请帮忙!

更新!在我的 Dao 中我得到了这个错误

enter image description here

那么如何让@Query 理解它我给它一个 java 字符串而不是 Kotlin 字符串

另见堆栈跟踪:

/home/Desktop/MySendBird/app/build/tmp/kapt3/stubs/debug/ChannelRoomDatabase.java:6: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
public abstract class ChannelRoomDatabase extends android.arch.persistence.room.RoomDatabase {
^
:app:compileDebugKotlin
w: /home/Desktop/MySendBird/app/src/main/java/com/example/anna/mysendbird/repository/ChannelRepository.kt: (16, 13): Variable 'db' is never used
w: /home/Desktop/MySendBird/app/src/main/java/com/example/anna/mysendbird/repository/ChannelRepository.kt: (47, 23): Parameter 'channel' is never used
w: /home/Desktop/MySendBird/app/src/main/java/com/example/anna/mysendbird/view/MainActivity.kt: (179, 29): Parameter 'userId' is never used

查看类似问题:

using Room with kotlin, get exception when build the database

Null object reference when I try to build room

最佳答案

您收到 Android Room 架构目录错误

/home/Desktop/MySendBird/app/build/tmp/kapt3/stubs/debug/ChannelRoomDatabase.java:6: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
public abstract class ChannelRoomDatabase extends android.arch.persistence.room.RoomDatabase {

模式导出目录未提供给注释处理器

您需要将架构目录添加到您的应用程序的 gradle.build,如下所示,

android {

defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}

}

这将告诉您的 Room 注释处理器生成数据库架构并将其放在 "room.schemaLocation"

下提到的目录下

关于java - 在 kotlin 中首次构建 Room 数据库时如何修复异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51327866/

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