My Parcelable Class :
public class Category implements Parcelable{
int id;
String name;
Department department;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeParcelable(department, 10);
}
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
public Category createFromParcel(Parcel in) {
return new Category(in);
}
public Category[] newArray(int size) {
return new Category[size];
}
};
public Category(Parcel in){
id=in.readInt();
name=in.readString();
department= in.readParcelable(department.getClass().getClassLoader());
}
When i send object of category class to other activity, then writeToparcel() and createFromParcel() method got called, I observed NullPointerException at
department= in.readParcelable(department.getClass().getClassLoader());
But while debugging i had checked that in writeToParel() method department object is stored correct, but how that is not returned back, in createFromParcel() , Same code is running fine in Android Studio 1.1 .
Currently I had changed my implementation code like that :
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeParcelable(department, Constants.PARCELABLE_DEPARTMENT);
dest.writeInt(department.getId());
dest.writeString(department.getName());
}
public Category(Parcel in){
id=in.readInt();
name=in.readString();
department=new Department(in.readInt(),in.readString());
}
Now, the code is working fine , but now i need to create extra department object, which i think is not good way .
Does any one have any solution regarding the problem ?
department.getClass().getClassLoader()
This is what throws the error. department == null and you're trying to fetch it's class. Therefore it throws an NPE.
Instead, fetch the class loader via the class object:
Department.class.getClassLoader()
Related
Hey guys ive found some tutorials about sending non primitive object to activity via intent. But see only that they have members of only primitive in all examples.
I have a class with members that are user data types.
How do i send an object with implementing Parcelable with non primitive instance variables like arraylist etc?
Thanks
The objects that are members of your class must also be Parcelable (or Serializable), and any objects they include must also be Parcelable (or Serializable). To summarize, a Parcelable object must have fields that are either: primitives, Parcelable objects (and their supported collections such as Map or ArrayList) or Serializable objects(and their supported collections such as Map or ArrayList).
A sample piece of code demonstrating this (the Foo class is a Parcelable which contains Bar, which is also Parcelable), is the following (in Java):
import android.os.Parcel;
import android.os.Parcelable;
public class Foo implements Parcelable {
private int primitive;
private Bar object;
public Foo() {
primitive = 0;
object = null;
}
private Foo(final Parcel in) {
primitive = in.readInt();
object = in.readParcelable(Bar.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(primitive);
dest.writeParcelable(object, flags);
}
public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {
public Foo createFromParcel(Parcel in) {
return new Foo(in);
}
public Foo[] newArray(int size) {
return new Foo[size];
}
};
}
and the Bar class:
import android.os.Parcel;
import android.os.Parcelable;
public class Bar implements Parcelable {
private String attribute;
public Bar() {
attribute = "";
}
private Bar(final Parcel in) {
attribute = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(attribute);
}
public static final Parcelable.Creator<Bar> CREATOR = new Parcelable.Creator<Bar>() {
public Bar createFromParcel(Parcel in) {
return new Bar(in);
}
public Bar[] newArray(int size) {
return new Bar[size];
}
};
}
I'm using the class Tests as the base class, the I created three more classes, Test1, Test2, Test3, they extends Tests class, then I have one more class, States which has an Arraylist.
States is used to gather a bunch of info including a list with the tests I want to perform, so I use the Arraylist and the method "add" to add test1, test2, or test3 to the list, then I want to send this State object to the activity B. I've implemented the parcelable interface on classes Test1, Test2, Test3 and States but I'm getting the next exception:
Unmarshalling unknown type code 6357090 at offset 300
Please, can suggest any way to achieve this, It's important to gather the tests on the arraylist, i think there lies the problem, thanks.
Sorry, this is too long for a comment, so I posted as an answer
Since Test1 extends Tests, Tests should haveit's own Parcelable implementation.
This implementation is the called by all its 'child' classes by using super. For example (this is what I use in my apps):
Tests class
public class Tests implements Parcelable {
private int Id;
private String Name;
// parcelable
protected Tests(Parcel in) {
Id = in.readInt();
Name = in.readString();
}
public static final Creator<Tests> CREATOR = new Creator<Tests>() {
#Override
public Tests createFromParcel(Parcel in) {
return new Tests(in);
}
#Override
public Tests[] newArray(int size) {
return new Tests[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(Id);
dest.writeString(Name);
}
}
Test1 class
public class Test1 extends Tests implements Parcelable {
private int Score;
// parcelable
protected Test1(Parcel in) {
super(in);
Score = in.readInt();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(Score);
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<Test1> CREATOR = new Creator<Test1>() {
#Override
public Test1 createFromParcel(Parcel in) {
return new Test1(in);
}
#Override
public Test1[] newArray(int size) {
return new Test1[size];
}
};
}
I am using the GCM network manager and I want to pass the service (specifically to the onRunTask(TaskParams taskParams) some objects. From the documentation taskParams are simply a string and a bundle but I want to pass more complex objects.
How can this be done?
Thank you!
One way is to have your custom object implement the Parcelable interface and use Bundle.putParcelable/Bundle.getParcelable.
It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster).
For example:
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
Also you can read Parcelable vs Serializable
I haven't understood how to create the code needed to implement correctly the Parcelable for an object that contains GregorianCalendar objects.
E.g. for an object User that contains String name; and GregorianCalendar creationDate;, my attempt is this:
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeParcelable(this.creationDate, flags);
}
private User(Parcel in) {
this.name = in.readString();
this.creationDate = in.readParcelable(GregorianCalendar.class.getClassLoader());
}
public static final Creator<User> CREATOR = new Creator<User>() {
public User createFromParcel(Parcel source) {
return new User(source);
}
public User[] newArray(int size) {
return new User[size];
}
};
that unfortunately doesn't work
in writeToParcel() at the line
dest.writeParcelable(this.creationDate, flags);
get writeParcelable cannot be applied to GregorianCalendar error
in
this.creationDate = in.readParcelable(GregorianCalendar.class.getClassLoader());
get Incompatible types error
How to code correctly the Parcelable?
EDIT
I have tried some code generators but use different ways and I'm not sure what is the right implementation, the first one use writeValue and readValue in this way:
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeValue(creationDate);
}
protected User(Parcel in) {
name = in.readString();
creationDate = (GregorianCalendar) in.readValue(GregorianCalendar.class.getClassLoader());
}
the second one use
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeSerializable(creationDate);
}
protected User(Parcel in) {
name = in.readString();
creationDate = (GregorianCalendar) in.readSerializable();
}
What is the right way?
You could use the second way since GregorianCalendar implements Serializable, and the Parcelable will work.
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeSerializable(creationDate);
}
protected User(Parcel in) {
name = in.readString();
creationDate = (GregorianCalendar) in.readSerializable();
}
However you MUST NOT serialize GregorianCalendar, because updates in GregorianCalendar related class can cause issue. Consider the case you load a file that contains GregorianCalendar objects created with a different API version, the difference in GregorianCalendar implementation will lead to sure errors, is enough a little difference in serialVersionUID costant to prevent the correct load of the file.
Use long parameters to store the millisecond of the date, if you don't want to rewrite your whole application you can easily create a couple of methods to convert from millis to GregorianCalendar and vice-versa as you need.
I'm implementing Parcelable class that has another Parcelable insde.
In OuterParcelable class:
#Override
public void writeToParcel(Parcel dest, int flags) {
Bundle tmp = new Bundle();
tmp.putParcelable("innerParcelable", mParcelable);
dest.writeBundle(tmp);
and then:
public OuterParcelable(Parcel parcel) {
super();
Bundle b = parcel.readBundle();
mParcelable = b.getParcelable("innerParcelable");
and:
public OuterParcelable createFromParcel(Parcel in) {
return new OuterParcelable(in);
}
When I recreate object using above code I get:
08-18 17:13:08.566: ERROR/AndroidRuntime(15520): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: my.package.InnerParcelable
A clean way to store non-primitive attributes as parcelable, possibly null, values. Use Parcel.writeValue() and readValue(). See comments in code below:
public class MyParcelableClass implements Parcelable {
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(getIntegerAttribute()); // getIntegerAttribute() returns Integer
dest.writeValue(getDoubleAttribute());
dest.writeValue(getMyEnumAttribute()); // getMyEnumAttribute() returns a user defined enum
dest.wrtieValue(getUserClassAttribute()); //UserClass must implement Parcelable in a similar fashion
}
private MyParcelableClass(Parcel in) {
setIntegerAttribute((Integer)in.readValue(null)); //pass null to use default class loader. Ok for Integer, String, etc.
setDoubleAttribute((Double)in.readValue(null)); //Cast to your specific attribute type
setEnumAttribute((MyEnum)in.readValue(null));
setUserClassAttribute((UserClass)in.readValue(UserClass.class.getClassLoader())); //Use specific class loader
}
#Override
public int describeContents() ...
public static final Parcelable.Creator<ParcelableLocationBean> CREATOR ...
}
Works like a charm. writeValue() and readValue() encapsulate the dealing with possible nulls and type detection. From javadoc:
public final void writeValue (Object v) Flatten a generic object
in to a parcel. The given Object value may currently be one of the
following types: null, String, Integer, ... String[],
boolean[], ... Any object that implements the Parcelable protocol. ...
Why are you putting the value into a Bundle? Did you completely implement the parcelable in your class?
Parcelable Skeleton
public MyClass(Parcel in) {
readFromParcel(in);
}
//
// Parcelable Implementation
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(aParcelableClass, flags);
}
private void writeObject(Parcel dest, Object obj) {
if (obj != null) {
dest.writeInt(1);
dest.writeValue(obj);
} else {
dest.writeInt(0);
}
}
public void readFromParcel(Parcel in) {
aParcelableClass = in.readParcelable(ParcelableClass.class.getClassLoader());
}
private Object readObject(Parcel in) {
Object value = null;
if (in.readInt() == 1) {
value = in.readValue(null); // default classloader
}
return value;
}
public static final Parcelable.Creator<MyClass> CREATOR = new Parcelable.Creator<MyClass>() {
#Override
public MyClass createFromParcel(Parcel source) {
return new MyClass(source);
}
#Override
public MyClass[] newArray(int size) {
return new MyClass[size];
}
};
I added a few things to make null values more easily dealt with, but the principle is the same. You need the #Override items, constructor, and Creator.
If you're going to read and write a parcelable you will have issues if you specify null as the class loader.