Tag: 安卓室

如何使用Android Room Persistence Library将Column注释为NOT NULL

我的数据类看起来像这样 @Entity(tableName = “items”) data class Item( @ColumnInfo(name = “name”) var name: String = “”, @ColumnInfo(name = “room”) var room: String = “”, @ColumnInfo(name = “quantity”) var quantity: String = “”, @ColumnInfo(name = “description”) var description: String = “”, @PrimaryKey(autoGenerate = true) @ColumnInfo(name = “id”) var id: Long = 0 ) 房间使用SQLite,而SQLite在其数据库中支持NOT NULL列。 我尝试使用@NonNull注释列,但它没有效果。 有没有办法使房间数据库中的列不可空?

Android室数据库错误:未使用的参数:在@Query函数中的天数

我正在使用Room数据库,试图在我的Dao中写入一个查询,这将删除比特定天数更早的所有记录。 这是我来到: @Query(“DELETE FROM my_table WHERE dateFrom <= date('now','-:days day')") fun deleteAllOlderThan(days: Int) 但是,当我尝试构建我的项目时,在我的Gradle控制台中出现错误: 错误:未使用的参数:天 我正在使用Kotlin,所以它也告诉我这个: 错误:任务’:nexo:kaptDebugKotlin’的执行失败。 内部编译器错误。 查看日志了解更多详情 我的查询有什么问题? UPDATE 我也收到有关转换日期的错误。 这是我的转换器: class Converter { @TypeConverter fun fromTimestamp(value: Long?) = value?.let { Date(it) } @TypeConverter fun dateToTimestamp(date: Date?) = date?.time } 我也将其添加到我的数据库与注释: @TypeConverters(Converter::class) 这是我得到的具体错误: 无法弄清楚如何将这个字段保存到数据库中。 你可以考虑为它添加一个types转换器。 e:e:private final java.util.Date dateFrom = null; 概要 我试图使用密封类的转换器在房间里,它会导致问题,所以我决定保持我的日期参数为长。 […]

Room Persistance Library在插入和删除时致命错误11(SIGEGV)崩溃

我一直在试图在我的项目中使用房间数据库,但它一直在崩溃,我不能确定是什么原因是… 这是我的情况: 当用户在对话框中输入一个日志点并按下ok时,这个函数被调用 fun manualOkClicked() { /*Check if all fields are valid ( if they are, location is created and folderId is saved )*/ if (logpoint.canCreateLocation(context) && logpoint.canSelectFolder(view?.getSelectedFolder())) { DataSource.saveLogpoint(logpoint) } } 这里是我如何保存日志点: fun saveLogpoint(logpoint: UserLogpoint) { Thread({ mydb.logpointDao().insert(logpoint) }).start() } 而我的应用程序崩溃与此日志: 12-27 16:17:31.108 17932-17941/com.example.myproject.debug A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x61724474 […]

Android Room:查询中的每个绑定variables都必须具有匹配的方法

我用kotlin使用android持久库库房。 Dao看起来像这样 @Dao interface CountryDao { @Query(“SELECT * FROM countries”) fun loadAllCountried() : LiveData<List> @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertAll(products: List) @Query(“SELECT * FROM countries WHERE id = :countryId”) fun loadCountry(countryId: Int): LiveData @Query(“SELECT * FROM countries WHERE id = :countryId”) fun loadCountrySync(countryId: Int): CountryEntity } 这似乎对我很好,但我得到这个错误 Error: Each bind variable in the query must have […]

Android Room Persistences库和Kotlin

我正在尝试使用Kotlin和Room Persistance Library编写一个简单的应用程序。 我遵循Android Persistance codelab中的教程 。 这是我在Kotlin的AppDatabase类: @Database(entities = arrayOf(User::class), version = 1) abstract class AppDatabase : RoomDatabase() { abstract fun userModel(): UserDao companion object { private var INSTANCE: AppDatabase? = null @JvmStatic fun getInMemoryDatabase(context: Context): AppDatabase { if (INSTANCE == null) { INSTANCE = Room.inMemoryDatabaseBuilder(context.applicationContext, AppDatabase::class.java).allowMainThreadQueries().build() } return INSTANCE!! } @JvmStatic fun destroyInstance() […]

错误:实体类必须使用@Entity进行注释

我决定使用kotlin和Room library,而且我真的遇到了很多问题,并且因阅读参考文献和find解决方案而感到厌倦。我的数据类: @Entity data class HistorySong( @PrimaryKey var SongId: Int =0, @ColumnInfo(name = “song_name”) var songName: String=””, @ColumnInfo(name = “song_artist”) var songArtist: String=””, @ColumnInfo(name = “song_link”) var songLink: String=””, @ColumnInfo(name = “image_path”) var songImagePath: String=””, @ColumnInfo(name=”is_favoutire”) var songisFavourite: Boolean= false ) 我的堂课: @Dao interface HistorySongDao { @Delete fun deleteSong(historySongDao: HistorySongDao) @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg […]

类未find,androidTest中使用Android Studio 3.0.1,Room,Kotlin的空测试套件

我有一个运行我的androidTest的问题。 这是我在gradle中的设置: apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ apply plugin: ‘kotlin-kapt’ android { compileSdkVersion 26 defaultConfig { applicationId “com.blabla.shoppinglistapp” minSdkVersion 17 targetSdkVersion 26 versionCode 1 versionName “1.0” testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner” } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ } } } ext.daggerVersion = ‘2.11’ ext.roomVersion = ‘1.0.0’ ext.mockitoVersion = ‘2.11.0’ dependencies […]

Android室库错误:无法findsetter的字段。 (科特林)

我正在使用房间库,我有下面提到的实体: @Parcelize @Entity(tableName = “tb_option”) data class OptionsTable( var question_id: Int? = null, var option_id: Int? = null, var option: String? = null, var is_selected: Int? = null, @PrimaryKey(autoGenerate = true) var sr_no: Int = 0) : Parcelable 你可以看到我把所有的字段都声明为var但是仍然显示错误: error: Cannot find setter for field. e: e: private java.lang.Integer is_selected; e: ^ 请为此建议一些修复。 谢谢

Kotlin:如何将对象列表插入房间?

我正在试图在基本界面中定义常见的CRUD方法,如下所示: interface BaseDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun create(obj: I) @Insert(onConflict = OnConflictStrategy.REPLACE) fun createAll(objects: List) @Delete fun delete(obj: I) } Room的以下ProductDao接口从基本接口inheritance: @Dao interface ProductDao : BaseDao { // Specific methods } 当我编译fun createAll(objects: List)的定义fun createAll(objects: List)产生以下错误: 参数的types必须是用@Entity或其集合/数组注解的类。

无法获得Room DB与Kotlin一起运行

你可以在我的回购中find所有的代码: https : //github.com/NWuensche/BookNotes/tree/add-room-db 我得到以下错误: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.nwuensche.booknotes, PID: 22753 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nwuensche.booknotes/com.nwuensche.booknotes.MainActivity}: java.lang.RuntimeException: cannot find implementation for com.nwuensche.booknotes.model.AppDatabase. AppDatabase_Impl does not exist at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6186) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: java.lang.RuntimeException: cannot find […]