使用房间持久性库编写注释时出错 - Android



我正在使用Room Persistence Library在我的Android应用程序中创建一个数据库。

当尝试将一个名为 User 的类作为Entity时,我在User类上方添加了@Entity注释,但 android 工作室显示错误说cannot resolve symbol Entity

@Entity               <--------cannot resolve symbol Entity
public class User {
    @PrimaryKey                     <--------cannot resolve symbol PrimaryKey
    private String userName;
    private String userPhoneNumber;
}

我还尝试@PrimaryKey注释添加到类User数据成员之一,但遇到了同样的错误。

我在这里做错了什么?

来自 Gradle 控制台的日志

Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar]
Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:checkDebugManifest
:app:generateDebugBuildConfig
:app:prepareLintJar
:app:generateDebugResValues
:app:generateDebugResources
:app:mergeDebugResources
:app:createDebugCompatibleScreenManifests
:app:processDebugManifest
:app:splitsDiscoveryTaskDebug
:app:processDebugResources
:app:generateDebugSources
:app:preDebugAndroidTestBuild
:app:compileDebugAndroidTestAidl
:app:processDebugAndroidTestManifest
:app:compileDebugAndroidTestRenderscript
:app:generateDebugAndroidTestBuildConfig
:app:generateDebugAndroidTestResValues
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:splitsDiscoveryTaskDebugAndroidTest
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:mockableAndroidJar
BUILD SUCCESSFUL in 2m 38s
22 actionable tasks: 22 executed

格拉德尔构建

apply plugin: 'com.android.application'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.Nick.phonebook_database"
        minSdkVersion 15
        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'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

您还需要导入房间依赖项。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'android.arch.persistence.room:runtime:1.0.0-beta1'
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0-beta1"
}

添加依赖项后,单击"Sync Now"。

最新更新