Android房间显示错误AppDatabase_Impl不存在

时间:2017-12-12 05:10:01

标签: java android android-room

我正在开发一个应用程序并为数据库添加空间,但它显示错误

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {} com.app.android/com.app.android.activities.AttendanceActivity:   java.lang.RuntimeException:无法找到实现   com.app.android.db.AppDatabase。 AppDatabase_Impl不存在

在gradle文件中我添加了所有内容。我的gradle文件如下所示。我不知道。问题是什么。此代码适用于其他应用程序。但它在此应用程序中显示。请帮助我。

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
useLibrary 'org.apache.http.legacy'
compileSdkVersion 25
buildToolsVersion '26.0.2'
defaultConfig {
    applicationId 'com.app.android'
    minSdkVersion 14
    targetSdkVersion 25
    versionCode 16
//     edumia   versionCode 16
    versionName "2.0.5"
//       edumia  versionName "2.0.5"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    debug {
        debuggable true
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}
}
repositories {
jcenter()
maven {
    url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo'
}
maven {
    url 'https://dl.bintray.com/ayz4sci/maven/'
}

}

allprojects {
repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}
}

dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile fileTree(include: ['*.jar'], dir: 'libs')


compile group: 'org.apache.httpcomponents', name: 'httpclient-android', version: '4.3.5.1'

compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.facebook.shimmer:shimmer:0.1.0@aar'
compile 'com.afollestad.material-dialogs:core:0.9.3.0'
compile 'com.afollestad.material-dialogs:commons:0.9.3.0'
compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.uphyca:stetho_realm:2.0.0'
compile 'com.evernote:android-job:1.1.11'
compile 'com.firebase:firebase-jobdispatcher:0.6.0'
compile 'com.android.support:design:25.0.0'
compile 'com.ss.bannerslider:bannerslider:1.6.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.ayz4sci.androidfactory:downloadprogress:1.0.2'
compile('com.alibaba.android:ultraviewpager:1.0.6@aar') {
    transitive = true
}


compile 'android.arch.lifecycle:extensions:1.0.0-rc1';
compile 'android.arch.persistence.room:runtime:1.0.0-rc1';
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-rc1';
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-rc1';
}

MainActivity.java

public class AttendanceActivity extends ActionBarActivity {
    private Class_ class_;
    private AppDatabase database;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            database = AppDatabase.getDatabase(getApplicationContext());
            database.classDao().removeAllUsers();

            Class_ aClass = new Class_(1,"model");
            database.classDao().addClass(aClass);

            Log.d(TAG, "added success");

}

AppDatabase.java

public abstract class AppDatabase extends RoomDatabase{

private static AppDatabase INSTANCE;
public abstract ClassDao classDao();
public static AppDatabase getDatabase(Context context) {
    if (INSTANCE == null) {
       INSTANCE =
                Room.databaseBuilder(context, AppDatabase.class, "edumiadatabase")
//Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class)
                        // To simplify the exercise, allow queries on the main thread.
                        // Don't do this on a real app!
                        .allowMainThreadQueries()
                        // recreate the database if necessary
                        .fallbackToDestructiveMigration()
                        .build();


    }
    return INSTANCE;
}

public static void destroyInstance() {
    INSTANCE = null;
}
}

Class_.java

public class Class_ {
@PrimaryKey
public int id;
public String Class_;


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getClass_() {
    return Class_;
}

public void setClass_(String class_) {
    Class_ = class_;
}

public Class_(int id, String class_){
    this.id=id;
    this.Class_ = class_;
}
}

ClassDao.java

@Dao
public interface ClassDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void addClass(Class_ user);

    @Query("delete from user")
    void removeAllUsers();

}

3 个答案:

答案 0 :(得分:7)

我在AppDatabase.java中添加了@Database(entities = {Class_.class}, version = 1)。数据库类的@Database注释和实体的@Entity。我给出了正确的答案。然后问题就解决了。

答案 1 :(得分:4)

即使在我的

中添加了正确的依赖项后,我也遇到了同样的问题 我的应用模块的

build.gradle。我的问题是,我使用了不同的模块来分离数据库层和我已经将我的依赖关系转移到相应模块的build.gradle解决了我的问题

希望这对其他人有帮助!!!

答案 2 :(得分:0)

@数据库注释丢失

添加类似这样的注释

@Database(entities = [Articles::class], version = 1)
abstract class NewsDatabase : RoomDatabase() {
    abstract fun articlesDao(): ArticlesDao
}

对于kotlin,也将以下行添加到依赖项

kapt "android.arch.persistence.room:compiler:1.1.1"

我也正在使用kotlin,我的所有数据类成员都是不可为空的。 我将它们更改为可为空的类型后,效果很好