我创建了一个扩展RealmObject的类,每当我尝试运行该类时,我都会得到runTime异常,如下所示:
06-09 00:58:16.333 30719-30719 / com.khaalijeb.inkdrops E / AndroidRuntime:致命异常:主要 过程:com.khaalijeb.inkdrops,PID:30719 io.realm.exceptions.RealmMigrationNeededException:由于以下错误,需要迁移: - Class' transactionacknowledgedata'已被添加。 at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method) 在io.realm.internal.OsSharedRealm。(OsSharedRealm.java:171) at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:241) 在io.realm.BaseRealm。(BaseRealm.java:136) 在io.realm.BaseRealm。(BaseRealm.java:105) 在io.realm.Realm。(Realm.java:164) at io.realm.Realm.createInstance(Realm.java:435) 在io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:342) 在io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282) 在io.realm.Realm.getDefaultInstance(Realm.java:343) 在com.khaalijeb.inkdrops.Fragment.NewPendingFragment.onCreate(NewPendingFragment.java:147) 在android.support.v4.app.Fragment.performCreate(Fragment.java:2236) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1285) 在android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1085) 在android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976) 在android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95) 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2198) 在android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2155) 在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2034) 在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626) 在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1268) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1116) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642) 在android.view.View.measure(View.java:23167) 在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 在android.view.View.measure(View.java:23167) 在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 在android.view.View.measure(View.java:23167) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.view.View.measure(View.java:23167) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 在android.view.View.measure(View.java:23167) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:825) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:704) 在android.view.View.measure(View.java:23167) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.view.View.measure(View.java:23167) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:825) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:704) 在android.view.View.measure(View.java:23167) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在com.android.internal.policy.DecorView.onMeasure(DecorView.java:716) 06-09 00:58:16.334 30719-30719 / com.khaalijeb.inkdrops E / AndroidRuntime:在android.view.View.measure(View.java:23167) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2693) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1559) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1830) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1447) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7130) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:935) 在android.view.Choreographer.doCallbacks(Choreographer.java:747) 在android.view.Choreographer.doFrame(Choreographer.java:682) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:921) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6642) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我不知道为什么我会收到此错误。那里的任何人都面临这个问题吗?
答案 0 :(得分:1)
请记住,在SQLite中你想添加一个表,所以你必须这样做:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
int currentVersion = oldVersion;
if(currentVersion == 1) {
db.execSql("CREATE TABLE Blah(personId int, name varchar(255))");
currentVersion++;
}
...
}
等等?
Well Realm不是SQLite,但它仍然需要您修改架构以添加新的模型类并应用所需的架构更改。
因此,您需要定义一个迁移(see this whole section about it in the docs)来处理从X到Y的架构版本更改。
所以你创建了一个迁移:
public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
...
}
@Override
public int hashCode() { return Migration.class.hashCode(); }
@Override
public boolean equals(Object obj) { return obj != null && obj instanceof Migration; }
}
Bump架构版本并指定迁移到RealmConfiguration
Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
.schemaVersion(currentVersion+1) // probably a constant somewhere
.migration(new Migration())
.build());
您实际上需要使用添加新添加的类的DynamicRealm API编写迁移。
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if(oldVersion == 0) {
RealmObjectSchema transactionacknowledgedata =
schema.create("transactionacknowledgedata");
transactionacknowledgedata.addField("name", String.class);
oldVersion++;
}
}
好吧,或者你设置new RealmConfiguration.Builder().deleteIfMigrationNeeded().build()
,只要模型发生变化,你就会删除(并重新创建)Realm。