Taha Sami
Taha Sami

Reputation: 1697

I got error message when I try put some data inside room database

I created simple offline database using room but I got this error

Caused by: java.lang.RuntimeException: cannot find implementation for com.test.test.AppDatabase.SplashScreenActivity_AppDatabase_Impl does not exist

Code

@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'

I found many solutions on this website but all not working with me, Thank you.

Upvotes: 0

Views: 166

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

  1. First of all you have add annotationProcessor in your build.gradle file
annotationProcessor "androidx.room:room-compiler:2.2.0"
  1. Either make your AppDatabase class static inside SplashScreenActivity like:
@Database(entities = {Categories.class}, version = 1)
abstract static class AppDatabase extends RoomDatabase {
    abstract CategoriesDao categoriesDao();
}

Or Create separate file for AppDatabase rather than inner class

Upvotes: 2

Related Questions