Android Studio:无法创建另一个 RealmObject



所以我正在努力创建这个应用程序,我已经用 RealmObject 创建了一个类。但是,每当我尝试创建另一个类时,我的应用程序都会停止工作。这只发生在我尝试创建一个扩展 RealmObject 的类时。它不会以任何其他方式发生。我已经有一个使用 RealmObject 的类。另外,我检查了logcat,它没有抛出任何错误消息。我该怎么办?这是AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.abc.apps">
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".profiles" />
            <activity android:name=".view_profile" />
            <activity android:name=".EditProfile" />
            <activity android:name=".Quiz" />
            <meta-data android:name="com.facebook.sdk.ApplicationId"
                android:value="@string/facebook_app_id"/>
        </application>
    </manifest>

这是build.gradle (project: apps):-

        buildscript {
            repositories {
                google()
                jcenter()
            }
            dependencies {
                classpath 'com.android.tools.build:gradle:3.0.1'
                classpath "io.realm:realm-gradle-plugin:5.0.0"
                // NOTE: Do not place your application dependencies here; they belong
                // in the individual module build.gradle files
            }
        }
        allprojects {
            repositories {
                google()
                jcenter()
            }
        }
        task clean(type: Delete) {
            delete rootProject.buildDir
        }

这里是build.gradle(模块:app(

    apply plugin: 'com.android.application'
    apply plugin: 'realm-android'
    android {
        compileSdkVersion 26
        configurations.all { resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9' }
        defaultConfig {
            applicationId "com.abc.apps"
            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'
            }
        }
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:26.1.0'
        compile 'com.facebook.android:facebook-android-sdk:4.31.0'
        compile 'com.android.support:multidex:1.0.3'
        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'
    }

这是我的 RealmObject 类,它会导致错误:-

    import io.realm.RealmObject;
    /**
     * Created by Admin on 4/5/2018.
     */
    public class showdata extends RealmObject {
       String id;
    }

发生这种情况是因为数据库(领域(迁移。你需要首先创建一个实现 RealmMigration 和覆盖迁移方法的类。以下代码片段来自 Realm 文档

public class MyMigration implements RealmMigration {
  @Override
  public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
     // DynamicRealm exposes an editable schema
     RealmSchema schema = realm.getSchema();
     // Migrate to version 1: Add a new class.
     // Example:
     // public Person extends RealmObject {
     //     private String name;
     //     private int age;
     //     // getters and setters left out for brevity
     // }
     if (oldVersion == 0) {
        schema.create("Person")
            .addField("name", String.class)
            .addField("age", int.class);
        oldVersion++;
     }
     // Migrate to version 2: Add a primary key + object references
     // Example:
     // public Person extends RealmObject {
     //     private String name;
     //     @PrimaryKey
     //     private int age;
     //     private Dog favoriteDog;
     //     private RealmList<Dog> dogs;
     //     // getters and setters left out for brevity
     // }
     if (oldVersion == 1) {
        schema.get("Person")
            .addField("id", long.class, FieldAttribute.PRIMARY_KEY)
            .addRealmObjectField("favoriteDog", schema.get("Dog"))
            .addRealmListField("dogs", schema.get("Dog"));
        oldVersion++;
     }
  }
}

在应用程序类(扩展应用程序(中,您需要启动一个领域配置对象并设置领域缺省配置。

RealmConfiguration config = new RealmConfiguration.Builder()
    .schemaVersion(2) // Must be bumped when the schema changes
    .migration(new MyMigration()) // Migration to run instead of throwing an exception
    .build()
Realm.setDefaultConfiguration(config);

最后,您需要引用在清单中扩展应用程序的应用程序类,如下所示:

<application
            android:name=".YOURAPPLICATIONCLASSNAME"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
.
.
.
</application>

相关内容

最新更新