Obtention de l'exception ci-dessus en essayant d'accéder à la base de données Realm.

Voici mon code:

public Observable<RealmResults<ContactsModel>> getAllContacts() {
        RealmResults<ContactsModel> contactsModel = realm.where(ContactsModel.class).notEqualTo("id", PreferenceManager.getID(mContext)).equalTo("Exist", true).findAllSorted("Linked", Sort.DESCENDING, "username", Sort.ASCENDING).sort("Activate", Sort.DESCENDING);
        return Observable.just(contactsModel);
    }

J'avais essayé certaines de ces solutions mais je ne m'étais pas aidé.

Désactiver l'exécution instantanée

Organiser appliquer les plugins

Classe MyApplication

    Realm.init(this);

    private static RealmConfiguration getRealmDatabaseConfiguration() {
            return new RealmConfiguration.Builder().name(getInstance().getString(R.string.app_name) + PreferenceManager.getToken(getInstance()) + ".realm").deleteRealmIfMigrationNeeded().build();
        }

        public static Realm getRealmDatabaseInstance() {
            return Realm.getInstance(getRealmDatabaseConfiguration());
        }

        public static boolean DeleteRealmDatabaseInstance() {
            return Realm.deleteRealm(getRealmDatabaseConfiguration());
        }

Construire Gradle

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.sis.smartmessenger"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'

    //butter knife
    compile 'com.jakewharton:butterknife:8.6.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.7'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.orhanobut:logger:1.15'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'jp.wasabeef:glide-transformations:2.0.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'jp.wasabeef:picasso-transformations:2.1.0'
    compile 'com.github.hani-momanii:SuperNova-Emoji:1.1'
    compile 'com.googlecode.libphonenumber:libphonenumber:8.3.2'
    compile('io.socket:socket.io-client:0.8.3') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }


}

Classe ContactModel

public class ContactsModel implements RealmModel {
    @PrimaryKey
    private int id;
    private int contactID;
    private String username;
    private String phone;
    private String phoneTmp;
    private boolean Linked;
    private boolean Activate;
    private boolean Exist;
    private String image;
    private String status;
    private String status_date;
    @Expose
    private String userState;

    public ContactsModel() {

    }
    public String getPhoneTmp() {
        return phoneTmp;
    }

    public void setPhoneTmp(String phoneTmp) {
        this.phoneTmp = phoneTmp;
    }
    public String getUserState() {
        return userState;
    }

    public void setUserState(String userState) {
        this.userState = userState;
    }


    public boolean isActivate() {
        return Activate;
    }

    public void setActivate(boolean activate) {
        Activate = activate;
    }

    public boolean isExist() {
        return Exist;
    }

    public void setExist(boolean exist) {
        Exist = exist;
    }


    public int getContactID() {
        return contactID;
    }

    public void setContactID(int contactID) {
        this.contactID = contactID;
    }


    public String getStatus_date() {
        return status_date;
    }

    public void setStatus_date(String status_date) {
        this.status_date = status_date;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public boolean isLinked() {
        return Linked;
    }

    public void setLinked(boolean linked) {
        Linked = linked;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
4
Manish 24 juil. 2017 à 13:11

2 réponses

Vous n'avez pas ajouté d'annotation @RealmClass. Si vous utilisez implements RealmModel, vous devez également ajouter @RealmClass.

@RealmClass
public class ContactsModel implements RealmModel {

Consultez la documentation.

1
EpicPandaForce 24 juil. 2017 à 12:16

Si vous utilisez l'annotation @RealmModule, n'oubliez PAS d'appliquer les paramètres du module dans votre configuration:

val config = RealmConfiguration.Builder()
    .schemaVersion(DATABASE_VERSION)
    .name(DATABASE_NAME)
    .modules(YourRealmModule()) // IMPORTANT!
    .deleteRealmIfMigrationNeeded()
    .build()
Realm.setDefaultConfiguration(config)
0
TonnyL 9 juil. 2018 à 04:39