Multiple putextra Unmarshalling unknown type code - java

I want to pass a custom object between my activities. I implemented Parceable for this class and put it in a extra. It works like a charm. Except if I put some other extras. In this case the app crash with this error : Unmarshalling unknown type code.
I have 2 class : A and B. A has a private field B. et my class B has a nested enum Enum. I implemented Parceable for A, B and Enum.
There is my code, I have rewritten it to be more concise.
A.java
public class A implements Parcelable {
B objectB;
String myString;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(myString);
out.writeParcelable(objectB, flags);
}
public static final Parcelable.Creator<Adresse> CREATOR = new Parcelable.Creator<Adresse>() {
public A createFromParcel(Parcel in) {
return new Adresse(in);
}
public A[] newArray(int size) {
return new A[size];
}
};
private Adresse(Parcel in) {
myString = in.readString();
objectB = in.readParcelable(B.class.getClassLoader());
}
}
B.java
public class B implements Parceable {
Bitmap bitmap;
String otherString;
Enum enum;
public enum Enum implements Parceable {
ONE ("One")
TWO ("Two")
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(ONE.name);
out.writeString(TWO.name);
}
public static final Parcelable.Creator<Identifiant> CREATOR = new Parcelable.Creator<Enum>() {
public Enum createFromParcel(Parcel in) {
return Enum.values()[in.readInt()];
}
public Enum[] newArray(int size) {
return new Enum[size];
}
};
}
public B(Parcel in) {
readFromParcel(in);
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(otherString);
out.writeParcelable(bitmap, flags);
out.writeParcelable(enum, flags);
}
private void readFromParcel(Parcel in) {
otherString = in.readString();
bitmap = in.readParcelable(Bitmap.class.getClassLoader());
enum = in.readParcelable(Enum.class.getClassLoader());
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public B createFromParcel(Parcel in) {
return new B(in);
}
public B[] newArray(int size) {
return new B[size];
}
};
#Override
public int describeContents() {
return 0;
}
MyActivity.java
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
A objectA = new A();
bundle.putParcelable("A", a);
bundle.putInt("Int", 1); //If I comment this line it works
intent.putExtras(bundle);
startActivityForResult(intent, 1);

Related

How to implement Parcelable for double ArrayList?

I got a JSON with an array of doubles and another for Strings. I am trying to create a Parcelable model class with the relevant methods. When the methods are created automaticaly, no line for the doubles' array list is created.
I looked for relevant questions and information, but, surprisingly, couldn't find any. I also added a jar file that was supposed to assist: android-parcelable-intellij-plugin.jar. Don't know what it's supposed to do and couldn't find information on how to use it.
My question is what should I write in the methods in order to get the array of doubles in a List.
The code:
public class Country implements Parcelable {
...
private List<String> timezones;
private List<Double> latlng;
protected Country(Parcel in) {
timezones = in.createStringArrayList();
this.latlng = new ArrayList<>(); // from jsonschema2pojo.org
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(timezones);
//nothing for the double array
}
public List<Double> getLatlng() {
return latlng;
}
public void setLatlng(List<Double> latlng) {
this.latlng = latlng;
}
public List<String> getTimezones() {
return timezones;
}
public void setTimezones(List<String> timezones) {
this.timezones = timezones;
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
}
...
Thanks.
Try this below, it works for me :
public class Country implements Parcelable {
private List<String> timezones;
private List<Double> latlng;
public Country(List<String> timezones, List<Double> latlng) {
this.timezones = timezones;
this.latlng = latlng;
}
protected Country(Parcel in) {
timezones = in.createStringArrayList();
double[] doubleArray = in.createDoubleArray();
latlng = new ArrayList<>();
if (doubleArray != null) {
for (double ele : doubleArray) {
latlng.add(ele);
}
}
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(timezones);
double[] doubleArray = new double[latlng == null ? 0 : latlng.size()];
for (int i=0, len=latlng.size(); i<len; i++) {
doubleArray[i] = latlng.get(i);
}
dest.writeDoubleArray(doubleArray);
}
#Override
public String toString() {
return "Country{" +
"timezones=" + timezones +
", latlng=" + latlng +
'}';
}
}

Extended (custom) arrayList of custom objects parcelable return size 0

It's one day work with no solution,
I would to passed data beetween activity, I implement Parcelable but from called activity I receive the ListFileMedia with a size=0.
When I mistake?
Class ListFileMedia (Extended of ArrayList)
public class ListFileMedia extends ArrayList<FileMedia> implements Parcelable{
public ListFileMedia(){.....}
// for test i have cancel all methods and var, sure i tested also with...
//Interface Parcelable
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
//dest.writeInt(countIntestazioni);
// Add inner class
dest.writeParcelable(this.add(FileMedia),flags);
}
public ListFileMedia(Parcel in) {
}
public final static Parcelable.Creator <ListFileMedia>CREATOR = new Creator<ListFileMedia>() {
#Override
public ListFileMedia createFromParcel(Parcel source) {
return new ListFileMedia(source);
}
#Override
public ListFileMedia[] newArray(int size) {
return new ListFileMedia[size];
}
};
}
Custom objects in FileListMedia
public class FileMedia implements Comparable,Parcelable{
public String getPath() {return path;}
public String getBucket() {
return bucket;
}
public String getMimeType() { return mimeType; }
private String path;
private String bucket;
private String mimeType;
private int giorno;
private int mese;
private int anno;
public FileMedia(int giorno,int mese,int anno, String path,String bucket,String mimeType){
....
}
public int compareTo(Object o) {
.....
}
//Interface Parcelable
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.path);
dest.writeString(this.bucket);
dest.writeString(this.mimeType);
dest.writeInt(this.giorno);
dest.writeInt(this.mese);
dest.writeInt(this.anno);
}
public FileMedia(Parcel in) {
this.path = in.readString();
this.bucket=in.readString();
this.mimeType=in.readString();
this.giorno=in.readInt();
this.mese=in.readInt();
this.anno=in.readInt();
}
public final static Parcelable.Creator <FileMedia>CREATOR = new Creator<FileMedia>() {
#Override
public FileMedia createFromParcel(Parcel source) {
return new FileMedia(source);
}
#Override
public FileMedia[] newArray(int size) {
return new FileMedia[size];
}
};}
Call activity
ListFileMedia test=new ListFileMedia();
ArrayList arrayList=new ArrayList();
test.add(new FileMedia(10,12,2016,"t","t","t"));
b.putParcelable("list",test);
intent.putExtras(b);
startActivity(intent);
Called activity
Intent intent = this.getIntent();
Bundle b=intent.getExtras();
ListFileMedia listFileMedia (ListFileMedia)b.getParcelable("list"); //size==0 !!!
But if i passed only b.putParcelable("list",new FileMedia(10,12,2016,"t","t","t") work correctely!!
Sorry for my English..

Passing multiple Objects between activities (using parcelable)

So what I am trying to do is pass ObjectB from the first activity to the second. ObjectB contains another object, ObjectA. I am getting really confused as to how to implement this. Also, I am using a list in ObjectA because I am going to have multiple integer values instead of just the 1 in this example. Here is the first activity:
private ObjectB objB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
objB = new ObjectB();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("KEY", objB);
startActivity(intent);
}
and the second activity:
private ObjectB objB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
objB = new ObjectB();
TextView tv = (TextView) findViewById(R.id.TextView1);
Bundle b = getIntent().getExtras();
objB = b.getParcelable("KEY");
tv.setText(objB.obj.stage1Count);
}
Class for ObjectB:
public class ObjectB implements Parcelable{
public ObjectA obj;
public ObjectB() { obj = new ObjectA(); }
public ObjectA getObj() {
return obj;
}
public ObjectB(Parcel in) {
readFromParcel(in);
}
public void setObj(ObjectA obj) {
this.obj = obj;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(obj, flags);
}
private void readFromParcel(Parcel in) {
obj = in.readParcelable(ObjectA.class.getClassLoader());
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public ObjectB createFromParcel(Parcel in) {
return new ObjectB(in);
}
public ObjectB[] newArray(int size) {
return new ObjectB[size];
}
};
}
Class for ObjectA:
public class ObjectA implements Parcelable{
public int stage1Count = 1;
public ObjectA() { ; };
public ObjectA(Parcel in) {
readFromParcel(in);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
List<Integer> iList = new ArrayList<Integer>();
iList.add(stage1Count);
dest.writeList(iList);
}
private void readFromParcel(Parcel in) {
List<Integer> iList = new ArrayList<Integer>();
iList = in.readArrayList(Integer.class.getClassLoader());
stage1Count = iList.get(0);
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public ObjectA createFromParcel(Parcel in) {
return new ObjectA(in);
}
public ObjectA[] newArray(int size) {
return new ObjectA[size];
}
};
}
Try this in first activity:
List<ObjectB> list = new ArrayList<ObjectB>;
intent.putParcelableArrayListExtra("key", list);
And in second activity
List<ObjectB> list = new ArrayList<ObjectB>;
list = bundle.getParcelableArrayList("key");
From list get your objectB
This was a really dumb question but I figured it out after a really long time haha. The problem is tv.setText(objB.obj.stage1Count);because objB.obj.stage1Count is actually an integer but for some reason it does not ring any bells when you try to use it as a string, idk maybe its something with the parceing.

Reading Vector <customObject> parcelable

I have a problems getting parameters using parcelables. When I call getIntent().getExtras().getParcelable("map"); I have a problem because of a problem with a Vector.
I have 2 parcelable classes:
public class Point implements Parcelable{
private double x;
private double y;
private double z;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public Point(Parcel in ) {
readFromParcel( in );
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Point createFromParcel(Parcel in ) {
return new Point( in );
}
public Point[] newArray(int size) {
return new Point[size];
}
};
public void readFromParcel(Parcel in){
x = in.readDouble();
y = in.readDouble();
z = in.readDouble();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeDouble(x);
dest.writeDouble(y);
dest.writeDouble(z);
}
}
And Map:
public class Map implements Parcelable{
private Point start;
private Point finish;
private java.util.Vector <Point> travel;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public Map(Parcel in ) {
readFromParcel( in );
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Map createFromParcel(Parcel in ) {
return new Map( in );
}
public Map[] newArray(int size) {
return new Map[size];
}
};
public void readFromParcel(Parcel in){
start = in.readParcelable(Point.class.getClassLoader());
finish = in.readParcelable(Point.class.getClassLoader());
Point[] travel = (Point[]) in.readParcelableArray(Point.class.getClassLoader());
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(start, flags);
dest.writeParcelable(finish, flags);
dest.writeParcelableArray(travel.toArray(new Point[travel.size()]), flags);
}
}
Then, how can I receive travel? I always have problems in readParcelableArray.
Someone could help me please??
When is almost impossible to find a solution often the problem is the base of this solution.
In my case, the solution consists in using a List instead of a Vector.
Then, in the class Map, I changed the methods readFromParceo and writeToParcel like these:
public void readFromParcel(Parcel in){
start = in.readParcelable(Point.class.getClassLoader());
finish = in.readParcelable(Point.class.getClassLoader());
travel = in.readArrayList(Point.class.getClassLoader());
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(start, flags);
dest.writeParcelable(finish, flags);
dest.writeList(travel);
}
I hope it'll help somebody in the future.

BadParcelableException: Launcher shortcut

I'm trying to pass in a custom Object into a shortcut I'm installing on the home screen, but I'm receiving a BadParcelableException when as soon as the shortcut is created. I've tried calling Bundle.setClassLoader, but the error persists.
Is there just a limitation with launchers that support home screen shortcuts?
public class Foo implements Parcelable {
public String title;
private Foo(Parcel in) {
title = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
}
public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {
#Override
public Foo createFromParcel(Parcel in) {
return new Foo(in);
}
#Override
public Foo[] newArray(int size) {
return new Foo[size];
}
};
}
public static void installShortcut(Context context, Foo foo) {
final Bundle args = new Bundle();
args.setClassLoader(Foo.class.getClassLoader());
args.putParcelable(EXTRA_FOO, foo);
final Intent detail = new Intent(context, FooActivity.class);
detail.putExtras(args);
final Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, detail);
...
context.sendBroadcast(shortcut);
}

Categories

Resources