简体   繁体   English

无法在我的测试应用程序中使用 Room 构建应用程序

[英]Unable to build application with Room in my test App

When I try to build the app it gives me an error当我尝试构建应用程序时,它给了我一个错误

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

In build.gradle I specified在 build.gradle 我指定

apply plugin: 'kotlin-kapt'

and

implementation "android.arch.persistence.room:runtime:1.1.1"
kapt 'android.arch.persistence.room:compiler:1.1.1'

My database class我的数据库 class

@Database(entities = [WeatherOfCities::class], version = 1, exportSchema = false)
public abstract class AppDatabase : RoomDatabase(){
    public abstract fun weatherOfCitiesDao(): WeatherOfCitiesDao
    companion object {
        private var INSTANCE: AppDatabase? = null
        fun getDatabase(context: Context): AppDatabase {
            if (INSTANCE == null) {
                synchronized(this) {
                    INSTANCE =
                        Room.databaseBuilder(context, AppDatabase::class.java, "database")
                            .build()
                }
            }
            return INSTANCE!!
        }
    }
}

My entity class我的实体 class

@Entity
data class WeatherOfCities (
    @PrimaryKey(autoGenerate = true)
    val id: Long,
    val city: String,
    val weather: Int
)

My Dao interface我的道界面

@Dao
interface WeatherOfCitiesDao {
    @Query("SELECT * FROM weatherOfCities")
    fun getAll(): List<WeatherOfCities>
    @Insert
    fun insert(weatherOfCities: WeatherOfCities)
    @Update
    fun update(weatherOfCities: WeatherOfCities)
}

And Build db in MainActivity并在 MainActivity 中构建数据库

class MainActivity : AppCompatActivity(), MainView {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var presenter = (application as MVPApplication).mainPresenter
        presenter?.attachView(this)
        var db = AppDatabase.getDatabase(this)
        var weatherOfCitiesDao = db.weatherOfCitiesDao()
    }
}

Why is the application not building and is it due to errors in the application code?为什么应用程序没有构建,是由于应用程序代码中的错误吗?

You need to add the ktx dependency eg您需要添加 ktx 依赖项,例如

implementation 'androidx.room:room-ktx:1.1.1'

I'd also suggest using 2.4.1 for all the room dependencies rather than 1.1.1我还建议对所有房间依赖项使用 2.4.1 而不是 1.1.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLite/Android Room 是否适合我的应用程序? - Is SQLite/Android Room the right database for my app? Android 构建不断失败,Room 和 Kapt 任务失败:app:kaptDebugKotlin - Android build keeps failing with Room and Kapt with failed task :app:kaptDebugKotlin 将实体插入我的房间数据库后应用程序崩溃 - app crashing after insert entity to my room database 构建室期间出现StackOverflowError - StackOverflowError during build room 在房间版本1.1.1中创建复合主键时,无法使用Kotlin编译器构建android studio项目 - Unable to build android studio project with Kotlin compiler while creating composite primary key in room version 1.1.1 将第二个 Room 数据库添加到我的应用程序会导致异常:“原因:java.lang.IllegalStateException:Room 无法验证数据完整性” - Adding second Room database to my app causes exception: " Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity" 无法在 Room 数据库中建立关系 - Cannot build relations in Room database ROOM 构造函数在构建时无法匹配 - ROOM constructors failed to match on build 无法将数据插入 Room 数据库 - Unable to insert data to Room database 如何使用房间在我的应用程序中存储基本的D&D统计数据,以便可以编辑和检索它们 - how to store basic D&D stats within my app using room, in such a way that they can be edited and retrieved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM