Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Realm - Passing Realm object using Intent

I want to pass a realm object from one activity to another. e.g.

Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("Student", studentObj); // studentObj is a realm object
startActivity(intent);

And receive it from the Second activity

Intent i = getIntent();
student = (Student) i.getSerializableExtra("Student");

but this causes a null pointer exception.

java.lang.RuntimeException: Unable to start activity ComponentInfo{testapp.com.tms/tms.testapp.com.tms.view.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.internal.LinkView io.realm.internal.Row.getLinkList(long)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5944)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

Any idea to fix this?

like image 780
Ezio Avatar asked Jul 03 '15 01:07

Ezio


1 Answers

Realm currently doesn't support parsing RealmObjects across Intents natively. So you have 2 options :

  1. Send some identifier instead and requery for the object on the other side.

  2. Use a 3rd party library like Parceler. You can see how here: https://realm.io/docs/java/latest/#parceler

like image 140
Christian Melchior Avatar answered Oct 04 '22 02:10

Christian Melchior