Я получаю сообщение об ошибке при попытке поместить некоторые данные в базу данных комнаты - PullRequest
0 голосов
/ 23 октября 2019

Я создал простую автономную базу данных, используя комнату, но получил эту ошибку

Причина: java.lang.RuntimeException: не удается найти реализацию для com.test.test.AppDatabase.SplashScreenActivity_AppDatabase_Impl не существует

Код

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    AppDatabase appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "testDatabase").build();
    Categories category = new Categories();
    category.setId(0);
    category.setId(1);
    category.setCategoryName("Te");
    category.setCategoryName("Test");
    appDatabase.categoriesDao().insertAll(category);
}

@Entity
class Categories {

    @PrimaryKey
    int id;

    @ColumnInfo(name = "category_name")
    String categoryName;

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

    void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

}

@Dao
public interface CategoriesDao {

    @Insert
    void insertAll(Categories... categories);

}

@Database(entities = {Categories.class}, version = 1)
abstract class AppDatabase extends RoomDatabase {
    abstract CategoriesDao categoriesDao();
}

Build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.**********"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:multidex:1.0.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //Material Design
    implementation 'com.google.android.material:material:1.2.0-alpha01'
    //Butter Knife
    implementation 'com.jakewharton:butterknife:10.2.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
    //Volley
    implementation 'com.android.volley:volley:1.1.1'
    //Room
    implementation "androidx.room:room-runtime:2.2.0"
}

apply plugin: 'com.jakewharton.butterknife'

Я нашел много решений на этом сайте, новсе не работают со мной, спасибо.

1 Ответ

2 голосов
/ 23 октября 2019
  1. Прежде всего, добавьте annotationProcessor в ваш build.gradle файл
annotationProcessor "androidx.room:room-compiler:2.2.0"
Либо сделайте ваш AppDatabase класс статичным внутри SplashScreenActivity, например:
@Database(entities = {Categories.class}, version = 1)
abstract static class AppDatabase extends RoomDatabase {
    abstract CategoriesDao categoriesDao();
}

Или Создайте отдельный файл для AppDatabaseчем внутренний класс

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...