【问题标题】:Android Studio: Unable to create another RealmObjectAndroid Studio:无法创建另一个 RealmObject
【发布时间】:2018-04-05 14:00:01
【问题描述】:

所以我正在创建这个应用程序,并且我已经使用 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 (Module: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;
    }

【问题讨论】:

  • 能否请您出示代码。因为没有代码很难说任何话。
  • @RishabhRawat 好的,我将在其中编辑它
  • @RishabhRawat 我添加了
  • 请给出错误名称,因为您的代码完全正确。如果您的设备显示 ANR,请检查日志猫窗口。因为没有任何错误,应用程序如何停止工作。
  • 见我之前的帖子。如果您愿意,请投票,因为您的问题可能会在我之前关于领域数据库的帖子中得到解决。 stackoverflow.com/questions/48880438/…

标签: android android-studio realm


【解决方案1】:

这是因为数据库(领域)迁移而发生的。 您首先需要创建一个实现 RealmMigration 并覆盖 migrate 方法的类。 以下代码 sn-p 来自领域documentation

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 的 Application 类,如下所示:

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

【讨论】:

  • @buffyBTVS96 logcat 中有消息吗?
  • 不:/你认为如果我把我的应用程序发给你,你可以看看有什么问题。我不明白
  • 将它上传到 git 存储库并给我发一个链接以找出问题所在。
猜你喜欢
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2018-04-28
  • 1970-01-01
相关资源
最近更新 更多