在六月等一场雪 2018-10-23 06:51 采纳率: 50%
浏览 1759
已采纳

Realm.getDefaultInstance()报错Class '类名' has been added.

我在原来的realm数据库上新增一个PageData2表然后配置了migrate,但是在调用getDefaultInstance方法的时候还是报错
java.lang.RuntimeException: Unable to pause activity {tv.mchang.iptv/tv.mchang.app.SplashActivity}: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Class 'PageDate2' has been added.
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3048)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3003)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2981)
at android.app.ActivityThread.access$1000(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
at dalvik.system.NativeStart.main(Native Method)
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Class 'PageDate2' has been added.
at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
at io.realm.internal.OsSharedRealm.(OsSharedRealm.java:171)
at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:241)
at io.realm.BaseRealm.(BaseRealm.java:136)
at io.realm.BaseRealm.(BaseRealm.java:105)
at io.realm.Realm.(Realm.java:164)
at io.realm.Realm.createInstance(Realm.java:435)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:342)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
at io.realm.Realm.getDefaultInstance(Realm.java:343)
at tv.mchang.data.realm.statistics.StatisticsDataUtils.addPageData(StatisticsDataUtils.java:17)
at tv.mchang.app.SplashActivity.saveStatisticsData(SplashActivity.java:216)
at tv.mchang.app.SplashActivity.onPause(SplashActivity.java:211)
at android.app.Activity.performPause(Activity.java:5351)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
at com.odin.framework.hack.InstrumentationHook.callActivityOnPause(InstrumentationHook.java:352)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3034)
... 12 more
具体代码如下:
在Application初始化了Realm并设置了setDefaultConfiguration,原来schemaVersion为8,我改为9
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.directory(realmDir)
.name("DataCenter.realm")
.schemaVersion(9)
.migration(new McMigration())
.build();
Realm.setDefaultConfiguration(realmConfiguration);
然后在migrate新增
if (oldVersion == 8) {
realmSchema.create("PageData2")
.addField("pageName", String.class)
.addField("stayTime", long.class)
.addField("visitTimes", int.class);
}
原本每次新增的时候应该在结尾oldVersion++的,但是之前维护人员在版本为5之后就没有oldVersion++了,我应该怎么修复?具体情况如下:
if (oldVersion == 5) {
realmSchema.get("OpusPublishInfo")
.renameField("opusId", "publishId")
.addField("mcId", long.class)
.addField("songName", String.class)
.addField("singer", String.class);
oldVersion++;
}
if (oldVersion == 6) {
realmSchema.get("VisitData")
.removeField("visitTimes")
.addField("visitTime", long.class);
realmSchema.create("OrderEvent")
.addField("videoId", String.class)
.addField("videoType", String.class)
.addField("eventTime", long.class);
}
if (oldVersion == 7) {
realmSchema.remove("AccountInfo");
realmSchema.remove("McUserInfo");
}
if (oldVersion == 8) {
realmSchema.create("PageData2")
.addField("pageName", String.class)
.addField("stayTime", long.class)
.addField("visitTimes", int.class);
}
然后新建类:
public class PageDate2 extends RealmObject {

String pageName;

long stayTime;
int visitTimes;

public PageDate2(){

}

public PageDate2(String pageName,long stayTime,int visitTimes){
    this.pageName = pageName;
    this.stayTime = stayTime;
    this.visitTimes = visitTimes;
}
    最后调用:
    public static void addPageData(String pageName, long stayTimeMills) {
    try (Realm realm = Realm.getDefaultInstance()) {
        PageDate2 pageDate2 = new PageDate2(pageName,stayTimeMills,1);
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm1) {
                realm1.copyToRealmOrUpdate(pageDate2);
            }
        });
    }
}
    报错!请问我在哪里出了问题?应该如何改正呢?求大佬指教!我上网查了很多但是还是找不到解决办法。
  • 写回答

2条回答 默认 最新

  • 在六月等一场雪 2018-10-23 07:21
    关注

    原因是然后在migrate新增realmSchema.create("PageData2")的表名和public class PageDate2 extends RealmObject类名不一致!坑爹的!细节啊细节,狗屎蒙了眼!

    再次更新:原因是我安装的“A”APP里面有创建名为"BehaviorData"的Realm数据表,在覆盖安装“B”APP的时候新增“BehaviorData”数据表会报错,原因就是Class '类名' has been added.所以只需要把“B”应用卸载掉再安装就可以了。不行的话把“A”也卸载掉。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动