Reputation: 3651
I want to send class Object to another activity which contains Latlng. But it gives exception. Below is my class which has latlng field.
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Keep;
import com.google.android.gms.maps.model.LatLng;
import java.io.Serializable;
public class Institute implements Serializable,Parcelable {
private String instituteName;
private LatLng latLng;
protected Institute(Parcel in) {
instituteName = in.readString();
latLng = in.readParcelable(LatLng.class.getClassLoader());
}
public static final Creator<Institute> CREATOR = new Creator<Institute>() {
@Override
public Institute createFromParcel(Parcel in) {
return new Institute(in);
}
@Override
public Institute[] newArray(int size) {
return new Institute[size];
}
};
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(instituteName);
parcel.writeParcelable(latLng,PARCELABLE_WRITE_RETURN_VALUE);
}
public String getInstituteName() {
return instituteName;
}
public void setInstituteName(String instituteName) {
this.instituteName = instituteName;
}
public void setLatLng(LatLng latLng) {
this.latLng = latLng;
}
public LatLng getLatLng() {
return latLng;
}
}
And it gives me following exception
FATAL EXCEPTION: main
Process: in.thoughtsmith.jink, PID: 24906
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.thoughtsmith.jink/in.thoughtsmith.jink.InstituteDetails}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: Seed0004
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: Seed0004
at android.os.Parcel.readParcelableCreator(Parcel.java:2432)
at android.os.Parcel.readParcelable(Parcel.java:2358)
at in.thoughtsmith.jink.Institute.<init>(Institute.java:45)
at in.thoughtsmith.jink.Institute$1.createFromParcel(Institute.java:55)
at in.thoughtsmith.jink.Institute$1.createFromParcel(Institute.java:52)
at android.os.Parcel.readParcelable(Parcel.java:2367)
at android.os.Parcel.readValue(Parcel.java:2264)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2614)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:786)
at in.thoughtsmith.jink.InstituteDetails.onCreate(InstituteDetails.kt:34)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Please help me how can i send class which contains latlng to another activity. And also please let me know is there any better way to achieve same
Upvotes: 1
Views: 1525
Reputation: 8106
The Issue is that you implement Serializable and Parcelable.
Serializable is used to serialize data (non android way which is slower). Since LatLng implements parcelable you should only use Parcelable. Parcelable is like Serializeable but a faster way serializing simple datas. Serializable doesnt know how to use LatLng.
Make sure that you use the proper import since LatLng may be used in a different package aswell.
You should also use the flags used by the writeToParc method. Means:
@Override public void writeToParcel(Parcel dest, int flags) {
parcel.writeString(instituteName);
parcel.writeParcelable(this.latLng, flags);
}
protected Institute(Parcel in) {
this.instituteName = in.readString();
this.latLng = in.readParcelable(LatLng.class.getClassLoader());
}
public class Institute implements Parcelable {}
Difference between Serializable and Parcelable: Android: Difference between Parcelable and Serializable?
Upvotes: 0
Reputation: 1519
You need to extract your latitude and longitude from latLng
double lat = latlng.latitude;
double lng = latlng.longitude;
Then you can add it easely to your Parcelable
Upvotes: 0