Android领域 – 使用意图传递领域对象

我想从一个活动传递领域对象到另一个。 例如

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

并从第二个活动接收它

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

但是这会导致空指针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) 

任何想法解决这个问题?

Related of "Android领域 – 使用意图传递领域对象"

Realm目前不支持通过Intents本地parsingRealmObjects。 所以你有2个选项

1)发送一些标识符,而对另一边的对象进行查询。 2)使用Parceler等第三方库。 你可以在这里看到: https : //realm.io/docs/java/latest/#parceler

您的ROW对象为null,您正在调用getLinkList(long)。

检查ROW是否被序列化? 可能不是。

当我尝试将realmobject传递给另一个活动时遇到了一些问题。 所以,我创build了一个基于我的模型的DTO对象,这个实现了Parcelable 。 它的作品,但我知道这不是更好的方法。

查看实现:

我有两个实体,Student和Disciplines,如下面的代码。

 package turbiani.com.br.realmfordummiesandroid.model; import android.os.Parcel; import android.os.Parcelable; import java.io.Serializable; import java.util.ArrayList; import java.util.Random; import java.util.UUID; import io.realm.RealmList; import io.realm.RealmObject; import io.realm.annotations.Ignore; import io.realm.annotations.PrimaryKey; /** * Created by turbiani on 23/07/15. */ public class Student extends RealmObject { @PrimaryKey private int id; private String name; private RealmList<Discipline> disciplines; public Student(){ this.id = new Random().nextInt(Integer.MAX_VALUE); } public Student(String name, RealmList<Discipline> disciplines) { this.name = name; this.disciplines = disciplines; this.id = new Random().nextInt(Integer.MAX_VALUE); } private Student(Parcel p){ this.name = p.readString(); this.disciplines = new RealmList<Discipline>(); this.id = p.readInt(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public RealmList<Discipline> getDisciplines() { return disciplines; } public void setDisciplines(RealmList<Discipline> disciplines) { this.disciplines = disciplines; } public int getId() { return id; } public void setId(int id) { this.id = id; } } 

和纪律类。

 package turbiani.com.br.realmfordummiesandroid.model; import android.os.Parcel; import android.os.Parcelable; import java.io.Serializable; import io.realm.RealmObject; /** * Created by turbiani on 23/07/15. */ public class Discipline extends RealmObject { private String name; private String grade; private int studentId; public Discipline(){} public Discipline(String name, String grade, int studentId) { this.name = name; this.grade = grade; this.studentId = studentId; } private Discipline(Parcel p){ this.name = p.readString(); this.grade = p.readString(); this.studentId = p.readInt(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } } 

StudentDTO和DisciplineDTO实现Parcelable。

 package turbiani.com.br.realmfordummiesandroid.dto; import android.os.Parcel; import android.os.Parcelable; import java.util.ArrayList; import java.util.List; import turbiani.com.br.realmfordummiesandroid.model.Discipline; /** * Created by turbiani on 04/08/15. */ public class StudentDTO implements Parcelable{ private int studentId; private String studentName; private List<DisciplineDTO> studentDisciplines; public StudentDTO(){} private StudentDTO(Parcel p){ this.studentName = p.readString(); this.studentDisciplines = new ArrayList<DisciplineDTO>(); p.readTypedList(this.studentDisciplines, DisciplineDTO.CREATOR); this.studentId = p.readInt(); } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public List<DisciplineDTO> getStudentDisciplines() { return studentDisciplines; } public void setStudentDisciplines(List<DisciplineDTO> studentDisciplines) { this.studentDisciplines = studentDisciplines; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public static final Parcelable.Creator<StudentDTO> CREATOR = new Parcelable.Creator<StudentDTO>(){ public StudentDTO createFromParcel(Parcel in){ return new StudentDTO(in); } public StudentDTO[] newArray(int size){ return new StudentDTO[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(studentName); dest.writeTypedList(studentDisciplines); dest.writeInt(studentId); } } package turbiani.com.br.realmfordummiesandroid.dto; import android.os.Parcel; import android.os.Parcelable; /** * Created by turbiani on 04/08/15. */ public class DisciplineDTO implements Parcelable { private String disciplineName; private String disciplineGrade; private int studentId; public DisciplineDTO(){} private DisciplineDTO(Parcel p){ this.disciplineName = p.readString(); this.disciplineGrade = p.readString(); this.studentId = p.readInt(); } public String getDisciplineName() { return disciplineName; } public void setDisciplineName(String disciplineName) { this.disciplineName = disciplineName; } public String getDisciplineGrade() { return disciplineGrade; } public void setDisciplineGrade(String disciplineGrade) { this.disciplineGrade = disciplineGrade; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } goes to @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(disciplineName); dest.writeString(disciplineGrade); dest.writeInt(studentId); } public static final Parcelable.Creator<DisciplineDTO> CREATOR = new Parcelable.Creator<DisciplineDTO>(){ public DisciplineDTO createFromParcel(Parcel in){ return new DisciplineDTO(in); } public DisciplineDTO[] newArray(int size){ return new DisciplineDTO[size]; } }; } 

最后,我如何在我的活动中使用这个。

 studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { StudentAdapter adapter = new StudentAdapter(); Intent intent = new Intent(MainActivity.this, AddDiscipline.class); intent.putExtra("StudentDTO", adapter.adaptToStudentDTO(students.get(position))); startActivity(intent); } }); 

在AddDiscipline活动中,我得到了dto对象。

 @Override public void onResume() { super.onResume(); studentDisciplineList = (ListView) findViewById(R.id.studentDisciplineList); btnAddDiscipline = (Button) findViewById(R.id.btnAddDiscipline); final EditText disciplineName = (EditText) findViewById(R.id.txtDisciplineName); final EditText disciplineGrade = (EditText) findViewById(R.id.txtGrade); this.studentDTO = getIntent().getParcelableExtra("StudentDTO"); studentDisciplineList.setAdapter(getAdapterToStudentDisciplineList()); 

如果你想要完整的源代码,请进入https://github.com/Turbiani/RealmForDummiesAndroid