Friday 15 February 2013

java - Parcel, ClassLoader and Reading Null values -



java - Parcel, ClassLoader and Reading Null values -

i trying understand how read null values parcel properly. let's utilize case:

public class mymodel implements parcelable { private long id; private string name; public mymodel() {} public mymodel(parcel in) { readfromparcel(in); } @override public int describecontents() { homecoming 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writevalue(id); dest.writevalue(name); } public void readfromparcel(parcel in) { id = (long) in.readvalue(long.class.getclassloader()); name = (string) in.readvalue(string.class.getclassloader()); } }

for sake of question, let's take illustration of field name of string type.

i understand can utilize writestring instead of writevalue, writestring throw nullpointerexception if name null.

so real issue is, if name null , effort readvalue , casting null string throw exception.

what best approach read null values parcel in case? need check null every time effort read parcel? much if have lot of fields in model. there difference between passing null or classloader when reading? class loader returns empty/default object if value null when reading parcel?

the error this, happens when reading 1 of strings parcel:

java.lang.runtimeexception: unable start activity componentinfo{com.company.myactivity}: java.lang.runtimeexception: parcel android.os.parcel@42b82908: unmarshalling unknown type code 7209045 @ offset 1368

how supply classloader mymodel.class. getclassloader()?

java android parcelable parcel

No comments:

Post a Comment