Hello Android Developers,

Why do I  get java.lang.ClassCastException when I tried to cast back
an array of custom object that implements Parcelable?

Here is the custom class:

public class Placemark implements Parcelable {
        public Double latitude;
        public Double longitude;
        public Double distanceFromUser;
        public int accuracy;
        public String address;
        private static final int earthRadius = 6371;

        public static final Parcelable.Creator<Placemark> CREATOR = new
Parcelable.Creator<Placemark>() {
                public Placemark createFromParcel(Parcel in) {
                        return new Placemark(in);
                }

                public Placemark[] newArray(int size) {
                        return new Placemark[size];
                }
        };

        public Placemark(JSONObject jo) throws JSONException {
                this.address = jo.getString("address");
                JSONArray array = jo.getJSONObject("Point").getJSONArray(
                                "coordinates");
                this.latitude = array.getDouble(1);
                this.longitude = array.getDouble(0);
                this.accuracy = array.getInt(2);
        }

        public Placemark(JSONObject jo, Location userLocation)
                        throws JSONException {
                this.address = jo.getString("address");
                JSONArray array = jo.getJSONObject("Point").getJSONArray(
                                "coordinates");
                this.latitude = array.getDouble(1);
                this.longitude = array.getDouble(0);
                this.accuracy = array.getInt(2);

                // based on Haversine formula
                // (http://www.movable-type.co.uk/scripts/latlong.html)
                Double dLat = Math.toRadians(userLocation.getLatitude()
                                - this.latitude);
                Double dLon = Math.toRadians(userLocation.getLongitude()
                                - this.longitude);
                Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                                + Math.cos(Math.toRadians(this.latitude))
                                * 
Math.cos(Math.toRadians(userLocation.getLatitude()))
                                * Math.sin(dLon / 2) * Math.sin(dLon / 2);
                Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                this.distanceFromUser = earthRadius * c;
        }

        public Placemark(Parcel in) {
                readFromParcel(in);
        }

        @Override
        public String toString() {
                return this.address
                                + (this.distanceFromUser != null ? " ("
                                                + 
Math.rint(this.distanceFromUser) + " km)" : "");
        }

        public int describeContents() {
                // TODO Auto-generated method stub
                return 0;
        }

        public void writeToParcel(Parcel dest, int flags) {
                dest.writeDouble(latitude);
                dest.writeDouble(longitude);
                dest.writeInt(accuracy);
                dest.writeString(address);
        }

        public void readFromParcel(Parcel in) {
                latitude = in.readDouble();
                longitude = in.readDouble();
                accuracy = in.readInt();
                address = in.readString();
        }
}

------------------------------------------------------------------------------
and here's the code that throws the error:

Bundle lastSearchBundle = extras.getBundle("lastSearch");
mSearchResult= (Placemark[]) lastSearchBundle.getParcelableArray
("searchResult"); //-->this is the line that throws the error

------------------------------------------------------------------------------
and here's the error detail:

E/AndroidRuntime( 9449): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.xxx/com.xxx.Search}:
java.lang.ClassCastException: [Landroid.os.Parcelable;
E/AndroidRuntime( 9449):        at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2268)
E/AndroidRuntime( 9449):        at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2284)
E/AndroidRuntime( 9449):        at android.app.ActivityThread.access$1800
(ActivityThread.java:112)
E/AndroidRuntime( 9449):        at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1692)
E/AndroidRuntime( 9449):        at android.os.Handler.dispatchMessage
(Handler.java:99)
E/AndroidRuntime( 9449):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 9449):        at android.app.ActivityThread.main
(ActivityThread.java:3948)
E/AndroidRuntime( 9449):        at java.lang.reflect.Method.invokeNative
(Native Method)
E/AndroidRuntime( 9449):        at java.lang.reflect.Method.invoke
(Method.java:521)
E/AndroidRuntime( 9449):        at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime( 9449):        at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:540)
E/AndroidRuntime( 9449):        at dalvik.system.NativeStart.main(Native
Method)
E/AndroidRuntime( 9449): Caused by: java.lang.ClassCastException:
[Landroid.os.Parcelable;
E/AndroidRuntime( 9449):        at com.xxx.Search.onCreate(Search.java:96)
E/AndroidRuntime( 9449):        at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1127)
E/AndroidRuntime( 9449):        at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2231)
E/AndroidRuntime( 9449):        ... 11 more

---------------------------------------------------------------------
Can somebody point my mistake? Thank you.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to