I am developing new app but I am getting following exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MessengerActivity}: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors: - Class 'SaveMessage' has been added. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3237) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1929) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:209) at android.app.ActivityThread.main(ActivityThread.java:7021) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872) Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors: - Class 'SaveMessage' 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:103) at io.realm.Realm.(Realm.java:163) at io.realm.Realm.createInstance(Realm.java:499) at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:341) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:284) at io.realm.Realm.getDefaultInstance(Realm.java:407) at com.example.myapplication.MessengerActivity.onCreate(MessengerActivity.java:92) at android.app.Activity.performCreate(Activity.java:7650) at android.app.Activity.performCreate(Activity.java:7639) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1295) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077)
below is my code :
Realm.init(this);
realm = Realm.getDefaultInstance();
realm.beginTransaction();
User userModel = realm.createObject(User.class);
Integer userId = Integer.valueOf("0");
String userName = "Michael";
userModel.setId(userId);
userModel.setName(userName);
//userModel.setIcon("icon");
Integer userId1 = Integer.valueOf("1");
String userName1 = "Lily";
userModel.setId(userId1);
userModel.setName(userName1);
realm.commitTransaction();
RealmResults<User> userModels = realm.where(User.class).findAll();
for (User user : userModels) {
Integer getId = Integer.valueOf(user.getId());
String getName = user.getName();
realm.commitTransaction();
}
SaveMessage saveModel = realm.createObject(SaveMessage.class);
Integer saveId = Integer.valueOf("0");
String savedName = "Michael";
Integer saveIds = Integer.valueOf("1");
String savedNames = "Lily";
saveModel.setmId(saveId);
saveModel.setmUsername(savedName);
saveModel.setmContent("content");
saveModel.ismRightMessage("isRightMessage");
Date now = Calendar.getInstance().getTime();
saveModel.setDate(now);
realm.commitTransaction();
RealmResults<SaveMessage> savesModels = realm.where(SaveMessage.class).findAll();
for (SaveMessage saveMessage : savesModels) {
Integer getId = saveMessage.getId();
String getName = saveMessage.getUsername();
String getContent = saveMessage.getContent();
Date date = saveMessage.getDate();
String rightMessage = String.valueOf(saveMessage.ismRightMessage());
realm.commitTransaction();
}
my SaveMessage model class :
@RealmClass
public class SaveMessage extends RealmObject {
private int mId;
private String mUsername;
private String mContent;
private Date mCreatedAt;
private boolean mRightMessage;
private String mPictureString;
private String mType;
public SaveMessage(int id, String username, String content, Date createdAt, boolean isRightMessage) {
mId = id;
mUsername = username;
mContent = content;
mCreatedAt = createdAt;
mRightMessage = isRightMessage;
}
public SaveMessage() {
}
public int getId() {
return mId;
}
public String getUsername() {
return mUsername;
}
public String getContent() {
return mContent;
}
public Date getDate() {
return mCreatedAt;
}
public void setDate(Date createdAt) {
mCreatedAt = createdAt;
}
public boolean ismRightMessage() {
return mRightMessage;
}
public int getmId() {
return mId;
}
public void setmId(int mId) {
this.mId = mId;
}
public String getmUsername() {
return mUsername;
}
public void setmUsername(String mUsername) {
this.mUsername = mUsername;
}
public String getmContent() {
return mContent;
}
public void setmContent(String mContent) {
this.mContent = mContent;
}
public boolean ismRightMessage(String isRightMessage) {
return mRightMessage;
}
public void setmRightMessage(boolean mRightMessage) {
this.mRightMessage = mRightMessage;
}
public String getPictureString() {
return mPictureString;
}
public void setPictureString(String pictureString) {
mPictureString = pictureString;
}
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
}