-2

I have a Placemark class that extends from Location class, so I think in this situation I can't use "implements Serializable" here. If anyone know how to use Serializable here, please help me

public class Placemark extends Location

So I start to use Parcelable in other to pass my Placemark object to the second Activity

public class DataPass implements Parcelable{

private Placemark pm;

public DataPass() 
{}

public DataPass(Parcel p)
{
    pm = (Placemark) p.readValue(Placemark.class.getClassLoader());    //<~~ may cause problem here
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel p, int flags) {
    p.writeValue(pm);
}

public Placemark getPM() {
    return pm;
}

public void setPM(Placemark pm) {
    this.pm = pm;
}

public static Parcelable.Creator<DataPass> CREATOR = new Parcelable.Creator<DataPass>() {

    @Override
    public DataPass createFromParcel(Parcel source) {
        return new DataPass(source);
    }

    @Override
    public DataPass[] newArray(int size) {
        return new DataPass[size];
    }
};

}

Then from current activity I call putExtra(String, Parcelable) and startActivity.

DataPass data = new DataPass();
data.setPM(myPlacemark);
mapintent.putExtra("p", data);
startActivity(mapintent);

And to retrieve it

Bundle bd = getIntent().getExtras();
try {
    DataPass data = bd.getParcelable("p");    //<~~ Exception
    Placemark pm = data.getPM();
} catch (Exception e)
{
    Log.e("error", e.toString());
}

When I run this code I got an exception at DataPass data = bd.getParcelable("p");

cause: ClassCastException
detailMessage: Location cannot be cast to Placemark)

So it's the first problem again when a extends Placemark from Location. I've searched a lot of websites but still can't work out this situation

Tam Huynh
  • 2,026
  • 1
  • 16
  • 20

2 Answers2

1

1) Instead of

 p.writeValue(pm);

and

pm = (Placemark) p.readValue(Placemark.class.getClassLoader());

try to write:

 p.writeParcelable(pw, 0);

and

pm = (Placemark) p.readParcelable(Placemark.class.getClassLoader());

2) Is your class Placemark implements Parcelable interface?

  • my Placemark class now just extends from Location, but I have a DataPass class implements Parcelable like above, which has Placemark as a property. I try Placemark implements Parcelable and Placemark implements Serializable before but it seems don't work for me, look like Placemark extends Location makes this situation becomes more complicated – Tam Huynh May 12 '14 at 11:36
  • You have error cause Dalvik trying to convert object to Location cause your Placemark class extends of it and doesn't implement Parcelable interface. You will have this error till you don't implement Parcelable in Placemark. – Suvitruf - Andrei Apanasik May 12 '14 at 11:39
  • I 'll try again, last time I tried implements Parcelable, I got stuck when any constructor of subclass of Location must have a String provider as a parameter. Now I know how to fix this, thanks a lot – Tam Huynh May 12 '14 at 12:07
  • @TamHuynh nice to hear it ) – Suvitruf - Andrei Apanasik May 12 '14 at 12:08
0

You can create a subclass of Application and store the object there. Here's an example.

Gheo
  • 98
  • 6
  • I try to use a subclass of Appication and it totally works, thanks a lot. But then i think i should change to use static singleton method because my app has only 2 activity and creating new Application makes me feel it is 'too big'. Anyway the link you gave me helps a lot. – Tam Huynh May 12 '14 at 11:37
  • Glad I could help. Be careful with static variables. If the activity in which you have the static variable is ended, you won't be able to use the variable in the second activity. Also, you can set this answer as useful, other should know how you solved the problem. – Gheo May 13 '14 at 12:12