ClassNotFoundException when unmarshalling Custom Class - java

I've been encountering a strange ClassNotFoundException when unmarshalling Custom Class. This happens at
this.moon = in.readParcelable(Moon.class.getClassLoader());
What is strange is that it happens only on certain devices like Samsung and HTC. Any help will be greatly appreciated. Thank you.
My code snippet is below
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeParcelable(this.warnings, flags);
dest.writeTypedList(this.conditions);
dest.writeParcelable(this.districtForecasts, flags);
dest.writeParcelable(this.localForecasts, flags);
dest.writeParcelable(this.partDayForecasts, flags);
dest.writeParcelable(this.dailyExtremes, flags);
dest.writeParcelable(this.dailyObservations, flags);
dest.writeTypedList(this.historicalObservations);
dest.writeTypedList(this.images);
dest.writeParcelable(this.forecastRainDates, flags);
dest.writeParcelable(this.wavesModel,flags);
dest.writeParcelable(this.tides, flags);
dest.writeTypedList(this.scripts);
dest.writeParcelable(this.animator, flags);
dest.writeParcelable(this.almanac, flags);
dest.writeParcelable(this.marineForecast, flags);
dest.writeParcelable(this.moon, flags);
dest.writeString(this.tag);
dest.writeParcelable(this.snow,flags);
}
protected LocalWeather(Parcel in) {
super(in);
this.warnings = in.readParcelable(Warnings.class.getClassLoader());
this.conditions = in.createTypedArrayList(Condition.CREATOR);
this.districtForecasts = in.readParcelable(DistrictForecasts.class.getClassLoader());
this.localForecasts = in.readParcelable(LocalForecasts.class.getClassLoader());
this.partDayForecasts = in.readParcelable(PartDayForecasts.class.getClassLoader());
this.dailyExtremes = in.readParcelable(DailyExtremes.class.getClassLoader());
this.dailyObservations = in.readParcelable(DailyObservations.class.getClassLoader());
this.historicalObservations = in.createTypedArrayList(Condition.CREATOR);
this.images = in.createTypedArrayList(Images.CREATOR);
this.forecastRainDates = in.readParcelable(RainForecasts.class.getClassLoader());
this.wavesModel = in.readParcelable(Waves.class.getClassLoader());
this.tides = in.readParcelable(Tides.class.getClassLoader());
this.scripts = in.createTypedArrayList(Script.CREATOR);
this.animator = in.readParcelable(Animator.class.getClassLoader());
this.almanac = in.readParcelable(Almanac.class.getClassLoader());
this.marineForecast = in.readParcelable(MarineForecast.class.getClassLoader());
this.moon = in.readParcelable(Moon.class.getClassLoader());
this.tag = in.readString();
this.snow = in.readParcelable(Snow.class.getClassLoader());
}
My Moon class is below
public class Moon implements Parcelable {
List<MoonPhase> phases = new ArrayList<>();
public List<MoonPhase> getPhases() {
return phases;
}
public void setPhases(List<MoonPhase> phases) {
this.phases = phases;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(this.phases);
}
public Moon() {
}
protected Moon(Parcel in) {
this.phases = in.createTypedArrayList(MoonPhase.CREATOR);
}
public static final Parcelable.Creator<Moon> CREATOR = new Parcelable.Creator<Moon>() {
#Override
public Moon createFromParcel(Parcel source) {
return new Moon(source);
}
#Override
public Moon[] newArray(int size) {
return new Moon[size];
}
};
public MoonPhase getNextPhase() {
if (phases != null && phases.size() > 0) {
return phases.get(0);
} else {
return null;
}
}
}

For List you should use this way :
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(this.phases);
}
protected Moon(Parcel in) {
this.phases = new ArrayList<>();
in.readList(this.phases , MoonPhase.class.getClassLoader());
}
The point is using writeListinstead of writeTypedList

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 +
'}';
}
}

Custom Object intent Parcelable element arraylist

This code works but I do not pass the data contained in the arrayList.
public class Percorso implements Parcelable {
int id;
String nome;
double distanza;
int numero_commenti;
double valutazione;
ArrayList<Commento> commenti;
ArrayList<Cordinate> cordinate;
public Percorso(){
}
protected Percorso(Parcel in) {
id = in.readInt();
nome = in.readString();
distanza = in.readDouble();
numero_commenti = in.readInt();
valutazione = in.readDouble();
}
public static final Creator<Percorso> CREATOR = new Creator<Percorso>() {
#Override
public Percorso createFromParcel(Parcel in) {
return new Percorso(in);
}
#Override
public Percorso[] newArray(int size) {
return new Percorso[size];
}
};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public double getDistanza() {
return distanza;
}
public void setDistanza(double distanza) {
this.distanza = distanza;
}
public int getNumero_commenti() {
return numero_commenti;
}
public void setNumero_commenti(int numero_commenti) {
this.numero_commenti = numero_commenti;
}
public double getValutazione() {
return valutazione;
}
public void setValutazione(double valutazione) {
this.valutazione = valutazione;
}
public ArrayList<Commento> getCommenti() {
return commenti;
}
public void setCommenti(ArrayList<Commento> commenti) {
this.commenti = commenti;
}
public ArrayList<Cordinate> getCordinate() {
return cordinate;
}
public void setCordinate(ArrayList<Cordinate> cordinate) {
this.cordinate = cordinate;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(nome);
parcel.writeDouble(distanza);
parcel.writeInt(numero_commenti);
parcel.writeDouble(valutazione);
}
}
//
public class Cordinate implements Parcelable {
double latitudine;
double longitudin;
public Cordinate(){}
protected Cordinate(Parcel in) {
latitudine = in.readDouble();
longitudin = in.readDouble();
}
public static final Creator<Cordinate> CREATOR = new Creator<Cordinate>() {
#Override
public Cordinate createFromParcel(Parcel in) {
return new Cordinate(in);
}
#Override
public Cordinate[] newArray(int size) {
return new Cordinate[size];
}
};
public double getLatitudine() {
return latitudine;
}
public void setLatitudine(double latitudine) {
this.latitudine = latitudine;
}
public double getLongitudin() {
return longitudin;
}
public void setLongitudin(double longitudin) {
this.longitudin = longitudin;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeDouble(latitudine);
parcel.writeDouble(longitudin);
}
}
INTENT
Percorso percorsoClick = listaPercorsi.get(i);
Intent intent = new Intent(getBaseContext(), PercorsoActivity.class);
intent.putExtra("percorso", percorsoClick);
startActivity(intent);
GET INTENT
percorso = getIntent().getParcelableExtra("percorso");
System.out.println(percorso.getNome());
getNome() work because is not a arrayList.
instead get getCordinate not work.
percorso.getCordinate().size() // null
I hope it was clear. I have set the Parcelable implementation on all classes.
Thank you very much for the help
ArrayList<Cordinate> cordinate; is not being parcelled. You still have to take care of write/read in/from Parcel parcel. Eg
protected Percorso(Parcel in) {
id = in.readInt();
nome = in.readString();
distanza = in.readDouble();
numero_commenti = in.readInt();
valutazione = in.readDouble();
cordinate = new ArrayList<Cordinate>();
in.readList(cordinate,Cordinate.class.getClassLoader());
}
and
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(nome);
parcel.writeDouble(distanza);
parcel.writeInt(numero_commenti);
parcel.writeDouble(valutazione);
parcel.writeList(cordinate);
}

Multiple putextra Unmarshalling unknown type code

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);

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.

How to use Intent.putParcelableArrayListExtra() method and the Parcelable interface, I just want to transfer data in between two activity

Look at my code, wheather wrong in it.
Activity one:
Intent intent = new Intent(SendActivity.this, PickContactsActivity.class);
startActivityForResult(intent, 20);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
ArrayList<PickBean> cons = data.getParcelableArrayListExtra("data");
for (int i = 0; i < cons.size(); i++) {
JLog.e(LOG_TAG, "displayName:" + cons.get(i).displayName + "displayNumber" + cons.get(i).displayNumber);
}
}
}
Activity two:
Intent data = new Intent();
ArrayList<PickBean> cons = new ArrayList<PickBean>();
for (int i = 0; i < conData.size(); i++) {
cons.add(new PickBean(conData.get(i).displayName, conData.get(i).displayNumber));
}
}
data.putParcelableArrayListExtra("data", cons);
setResult(RESULT_OK, data);
finish();
The PickBean code:
public class PickBean implements Parcelable {
public String displayName;
public String displayNumber;
public boolean selected = false;
public PickBean() {
super();
}
public PickBean(String displayName, String displayNumber) {
super();
this.displayName = displayName;
this.displayNumber = displayNumber;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(displayName);
dest.writeString(displayNumber);
}
public final Parcelable.Creator<PickBean> CREATOR = new Parcelable.Creator<PickBean>() {
#Override
public PickBean createFromParcel(Parcel source) {
return new PickBean(source.readString(), source.readString());
}
#Override
public PickBean[] newArray(int size) {
return new PickBean[size];
}
};
}
It will always throws
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=20,
result=-1, data=Intent { (has extras) }} to activity
{com.chishacai.smscenter/com.chishacai.smscenter.SendActivity}: java.lang.NullPointerException: expected receiver of type
com.chishacai.smscenter.bean.PickBean, but got null
Caused by: java.lang.NullPointerException: expected receiver of type
com.chishacai.smscenter.bean.PickBean, but got null
Please help me how to deal with this problem, thanks.
Pass the data :
Intent intent = new Intent(SendActivity.this, PickContactsActivity.class);
Bundle bundle;
bundle.putParcelableArrayList("data", cons); // Be sure con is not null here
intent.putExtras(bundle);
Get the data :
ArrayList<PickBean> arrayParents = intent.getParcelableArrayListExtra("data");
Replace you method newArray with :
public final Parcelable.Creator<PickBean> CREATOR = new Parcelable.Creator<PickBean>() {
#Override
public PickBean createFromParcel(Parcel source) {
return new PickBean(source);
}
#Override
public PickBean[] newArray(int size) {
return new PickBean[size];
}
};
public PickBean(Parcel data) {
this.displayName = data.readString();
this.displayNumber = data.readString();
}
//DataModel Class
package com.DEECOUP.DataModel;
import android.os.Parcel;
import android.os.Parcelable;
public class OrderedData implements Parcelable {
private String _title, _subtitle, _person_type, _processed_name;
private Integer _price, _quantity;
public OrderedData(String title, String subtitle , String person_type, String processed_name,
Integer price, Integer quantity)
{
_title = title;
_subtitle =subtitle ;
_person_type = person_type;
_processed_name = processed_name;
_price = price;
_quantity = quantity;
}
public OrderedData(Parcel in) {
// TODO Auto-generated constructor stub
String[] data = new String[6];
in.readStringArray(data);
_title = data[0];
_subtitle = data[1];
_person_type= data[2];
_processed_name = data[3];
_price = Integer.parseInt(data[4]);
_quantity = Integer.parseInt(data[5]);
}
public String getTitle()
{
return _title;
}
public String getSubTitle()
{
return _subtitle;
}
public String getPersonType()
{
return _person_type;
}
public String getProcessedName()
{
return _processed_name;
}
public Integer getPrice()
{
return _price;
}
public Integer getQuantity()
{
return _quantity;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {
this._title,
this._subtitle,
this._person_type,
this._processed_name,
String.valueOf(this._price),
String.valueOf(this._quantity)
});
}
public static final Parcelable.Creator<OrderedData> CREATOR = new Parcelable.Creator<OrderedData>() {
public OrderedData createFromParcel(Parcel in) {
return new OrderedData(in);
}
public OrderedData[] newArray(int size) {
return new OrderedData[size];
}
};
}
//button click to send data on second activity
case R.id.btnOrderDetails:
Intent intent = new Intent(SenderActiviry.this,
RecieverActivity.class);
intent.putParcelableArrayListExtra("data", orderedDataList);
startActivity(intent);
break;
// Add data to datamodel from first addtivity
global list
ArrayList<OrderedData> orderedDataList = new ArrayList<OrderedData>();
and in on create
orderedDataList.add(new OrderedData(_title, _subTitle,_person_Type, _processName,_pRice,getFinalQuantity));

Categories

Resources