Android Room报错:Unresolved reference: onConflictStrategy及SQL高亮异常
Room Dao接口问题解决
问题1:@Insert注解中onConflictStrategy未解析,导入后仍提示未使用
原因及解决:
- 枚举类
OnConflictStrategy首字母需大写,你可能误用了小写的onConflictStrategy,正确调用应为OnConflictStrategy.IGNORE - 确保Dao接口导入正确的类:
import androidx.room.Insert import androidx.room.OnConflictStrategy
- 若导入后仍提示未使用,执行以下操作:
- 点击菜单栏
Build > Clean Project - 再执行
Build > Rebuild Project - 必要时重启Android Studio清除IDE缓存
- 点击菜单栏
问题2:@Query注解中SQL语句无关键字高亮
原因及解决:
- 首先确保Dao接口被
@Dao注解标记,Room插件依赖该注解识别SQL语句:
import androidx.room.Dao import androidx.room.Query @Dao interface NoteDao { @Query("SELECT * FROM notes_table ORDER BY id ASC") fun getAllNotes(): List<Note> }
- 检查Android Studio的Room插件是否启用:
- 打开
File > Settings > Plugins - 搜索Room,确认插件处于启用状态
- 打开
- 若插件已启用,清除IDE缓存:
点击File > Invalidate Caches...,选择Invalidate and Restart - 额外建议:SQL关键字使用大写格式(如
SELECT、FROM),更利于IDE识别语法
附用户提供的配置与代码
build.gradle配置:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-android' id 'kotlin-kapt' } android { namespace 'com.example.scribbles' compileSdk 33 defaultConfig { applicationId "com.example.scribbles" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } packagingOptions { exclude 'META-INF/atomicfu.kotlin_module' } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation "androidx.appcompat:appcompat:1.6.1" implementation "androidx.activity:activity-ktx:1.6.1" // Dependencies for working with Architecture components // You'll probably have to update the version numbers in build.gradle (Project) // Room components implementation "androidx.room:room-runtime:2.5.0" implementation "androidx.room:room-ktx:2.5.0" kapt "androidx.room:room-compiler:2.5.0" androidTestImplementation "androidx.room:room-testing:2.5.0" // Lifecycle components implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1" implementation "androidx.lifecycle:lifecycle-common-java8:2.5.1" // Kotlin components implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20" api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4" api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4" // UI implementation "androidx.constraintlayout:constraintlayout:2.1.4" implementation "com.google.android.material:material:1.8.0" // Testing testImplementation "junit:junit:4.13.2" androidTestImplementation "androidx.arch.core:core-testing:2.1.0" androidTestImplementation ("androidx.test.espresso:espresso-core:3.4.0", { exclude group: 'com.android.support', module: 'support-annotations' }) androidTestImplementation "androidx.test.ext:junit:1.1.5" }
Note实体类代码:
package com.example.scribbles import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey @Entity(tableName = "notes_table") class Note(@ColumnInfo(name = "text")val text:String) { @PrimaryKey(autoGenerate = true) var id = 0 }
内容的提问来源于stack exchange,提问作者Karan Kumar
相关产品推荐
相关产品推荐

