1

I cant resolve problem when i sending my object "filmovi" to another activity i got a error. when i was tried to send another object "korisnik" it works without any problem.

Error

FATAL EXCEPTION: main
java.lang.ClassCastException: ba.fit.kino.model.filmovi cannot be cast to android.os.Parcelable

Sending from activity

filmovi Film = ((filmovi)lstView.getItemAtPosition(position)); 
Intent intent = new Intent(getApplicationContext(), RezervacijaActivity.class)
intent.putExtra("Rezervacija", Film);
startActivity(intent);

Reciving in activity

filmovi filmoviRezervacija;

Bundle bundle = getIntent().getExtras();

if(bundle != null){
     filmoviRezervacija = bundle.getParcelable.("Rezervacija"); 
}

I RESOLVE PROBLEM WITHT THIS:

public class filmovi implements Parcelable{......


 public filmovi (Parcel source)
    {

        this.setFilmID(source.readInt());
        this.setNaziv(source.readString());
        this.setCijenaKarte(source.readFloat());
        this.setSalaID(source.readInt());

    }

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

    @Override
    public void writeToParcel(Parcel dest,int flags)
    {
        dest.writeInt(this.filmID);
        dest.writeString(this.naziv);
        dest.writeFloat(this.cijenaKarte);
        dest.writeInt(this.salaID);
    }


    public static final Parcelable.Creator<filmovi> CREATOR = new Parcelable.Creator<filmovi>() {
        @Override
        public filmovi createFromParcel(Parcel source) {
            return new filmovi(source);
        }

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


}
Srđan Marković
  • 125
  • 1
  • 2
  • 8

2 Answers2

0

EDIT: As kcoppock mentioned, you can't place Objects into Intents as extras unless they're serializable or parcelable. Therefore, your Film class will need to implement one of those. I've only ever added native types (int, etc.) to Intents, so I did not know this. Something to watch out for!

As an aside, it's good practice to extract your key strings to static final values. That ensures that the same string ends up in each location you use it.

Graph Theory
  • 699
  • 1
  • 7
  • 18
0

The reason is that your filmovi class is not parcelable

To make filmovi, or any class for that matter, parcelable, the class and all of its inner members should implement the parcelable interface, and implement a writeToParcel method which loosely speaking streams the class' content.

Here, for example

class MyClass implements parcelable {
    private MyMemberDataClass data;  <----- must also implement parcelable 

    void writeToParcel(Parcel dest, int flags) {...}
}

It is not enough that MyClass will implement parcelable.

MyMemberDataClass (i.e. the inner member class) must do so as well.


This may bet complicated. And in many cases it is also not really necessary...

instead, consider using an activity-parameters static object to which you will pass all of your activity's required params without the need to parcel them!:

filmovi Film = ((filmovi)lstView.getItemAtPosition(position)); 
Intent intent = new Intent(getApplicationContext(), RezervacijaActivity.class)
RezervacijaActivityParams.setValue(Film); <--------------- instead of putExtra()
startActivity(intent);

Where:

class RezervacijaActivityParams {
       private static filmovi Film;
       public static void getValue(filmovi f) { Film = f; }
       public static filmovi getValue() { return Film; }
}

and in RezervacijaActivity's onCreate:

class RezervacijaActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        filmovi = RezervacijaActivityParams.getValue();
        RezervacijaActivityParams.setValue(null); <---------- clear static data
    }

}

Note, and this is also an answer to kcoppock's comment, that it is a good practice for your activity to clear the static data immediately after retrieving it.

Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30
  • Using static data members is highly discouraged. Either persist it to disk somewhere, or make it properly serializable or parcelable. It's not too difficult to implement. – Kevin Coppock Jul 18 '14 at 18:14