英文:
Android Room - Unresolved reference: onConflictStrategy
问题
Dao接口图像
在这个地方
@Insert(onConflict = onConflictStrategy.IGNORE)
Android Studio 报错
未解决的引用: onConflictStrategy
以及在这个地方
@Query("Select * from notes_table order by id ASC")
整个字符串都是绿色的,而Select、from、order by 和 ASC 这些单词应该是橙色的。
以下是我的 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"
// 用于与架构组件一起使用的依赖项
// 您可能需要在 build.gradle(Project)中更新版本号
// Room 组件
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"
// 生命周期组件
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 组件
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"
// 用户界面
implementation "androidx.constraintlayout:constraintlayout:2.1.4"
implementation "com.google.android.material:material:1.8.0"
// 测试
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"
}
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
}
我已检查依赖项的版本是否是最新的,并导入了 androidx.room.OnconflictStrategy,但它显示未使用的导入指令。
英文:
Dao Interface Image
In this in the line
@Insert(onConflict = onConflictStrategy.IGNORE)
android studio says
Unresolved reference: onConflictStrategy
and in the line
@Query("Select * from notes_table order by id ASC")
The whole string is of green color while the words Select, from, order by and ASC should be orange
Here is my 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"
}
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
}
I checked the dependencies version are up to date and imported androidx.room.OnconflictStrategy
but it was saying unused import directive.
答案1
得分: 0
将 onConflictStrategy
更改为 OnConflictStrategy
,你就没问题了。
英文:
Change onConflictStrategy
to OnConflictStrategy
and yo'll be fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论