ClassNotFoundException when unmarshalling (Passing object between differente apps)

181 views Asked by At

I have a little problem about Parcelable objects and passing it between activities.

I have two different apps, Activity A request Activity B by an Intent with startActivityForResult, B Activity sends back a custom object (Class C) which is defined in both two projects. This C class is parcelable and it has defined all methods, but when I try to get back that object stored in a bundle, I get that exception.

This is my class C (Equipo).

public class Equipo implements Parcelable{

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

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

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

@Override
public void writeToParcel(Parcel parcel, int i) {
    localizacion.writeToParcel(parcel, i);
    parcel.writeString(nombre);
    parcel.writeString(url);
}

private void readFromParcel(Parcel in) {
    localizacion=Location.CREATOR.createFromParcel(in);
    nombre = in.readString();
    url = in.readString();
}

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

private String nombre;
private Location localizacion;
private String url;

public Equipo(String nombre, Location localizacion, String url) {
    this.nombre = nombre;
    this.localizacion = localizacion;
    this.url = url;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public Location getLocalizacion() {
    return localizacion;
}

public void setLocalizacion(Location localizacion) {
    this.localizacion = localizacion;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

}

In Activity A I use this Intent

Intent i = new Intent();
i.setAction(SELECCIONAR); // Activity B in the other project is ready to answer this Intent
startActivityForResult(i, LOCAL);

Then in Activity B I send back the object:

Intent returnIntent = new Intent();
Bundle b = new Bundle();
b.putParcelable("seleccion", seleccion);
returnIntent.putExtras(b);
setResult(ListaEquipos.RESULT_OK,returnIntent);
finish();

And finally get the object back, but when I get the parcelable object from bundle I get the exception:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    data.setExtrasClassLoader(Equipo.class.getClassLoader());
    if (requestCode == LOCAL) {
        if(resultCode == InsertaEquipos.RESULT_OK){

            Bundle b = data.getExtras();
            b.setClassLoader(Equipo.class.getClassLoader());
            if (b != null)
                A = b.getParcelable("seleccion"); //EXCEPTION
        }
    }
}//onActivityResult

Logcat tells me that it cannot find the class:

Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.example.alvaro.listaequipos.Equipo

I think it's caused because it's trying to find the class in that package, and that's from the other project.

Can anybody help me? Thanks :)

0

There are 0 answers