0

I'm trying to send an ArrayList of objects to another activity, but i got this error on the logcat, and i can't find a way to solve it, i'm a begginer.

GrupoMuscular class:

public class GrupoMuscular implements Parcelable{

    private int id;
    private static int id_aux = 0;
    private String nome;
    private int imagem;
    private List<Exercicio> exercicios;

    public GrupoMuscular(String nome, int img) {
        //super();
        id = id_aux;
        id_aux++;
        this.nome = nome;
        this.imagem = img;
        this.exercicios = new ArrayList<Exercicio>();
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getImagem() {
        return imagem;
    }

    public void setImagem(int imagem) {
        this.imagem = imagem;
    }

    public List<Exercicio> getGruposMuscular() {
        return exercicios;
    }

    public void addExercicio(int id, String nome, String descricao, int imagem){        
        Exercicio ex = new Exercicio(id, nome, descricao, imagem);
        exercicios.add(ex);
    }

    public void setGruposMuscular(List<Exercicio> gruposMuscular) {
        this.exercicios = gruposMuscular;
    }

    public List<Exercicio> getExercicios() {
        return exercicios;
    }

    public void setExercicios(List<Exercicio> exercicios) {
        this.exercicios = exercicios;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }
}

ExerciciosActivity: Here is when i'm trying to send the array, when i click on a item in the listview.

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent;

                        intent = new Intent(ExerciciosActivity.this, ListaExerciciosActivity.class);
                        intent.putParcelableArrayListExtra("gruposMusculares", gruposMusculares);
                        startActivity(intent);
                }});
            }

ListaExerciciosActivity: Here is where i want receive the array.

gruposMusculares = getIntent().getParcelableArrayListExtra("gruposMusculares");

Logcat:

01-03 18:24:08.889: E/AndroidRuntime(18813): FATAL EXCEPTION: main
01-03 18:24:08.889: E/AndroidRuntime(18813): Process: com.AMOV.mr.fit, PID: 18813
01-03 18:24:08.889: E/AndroidRuntime(18813): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AMOV.mr.fit/com.AMOV.mr.fit.ListaExerciciosActivity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.AMOV.mr.fit.GrupoMuscular
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Looper.loop(Looper.java:136)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.ActivityThread.main(ActivityThread.java:5144)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at java.lang.reflect.Method.invokeNative(Native Method)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at java.lang.reflect.Method.invoke(Method.java:515)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at dalvik.system.NativeStart.main(Native Method)
01-03 18:24:08.889: E/AndroidRuntime(18813): Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.AMOV.mr.fit.GrupoMuscular
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Parcel.readParcelableCreator(Parcel.java:2156)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Parcel.readParcelable(Parcel.java:2097)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Parcel.readValue(Parcel.java:2013)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Parcel.readListInternal(Parcel.java:2343)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Parcel.readArrayList(Parcel.java:1703)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Parcel.readValue(Parcel.java:2034)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Parcel.readArrayMapInternal(Parcel.java:2314)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Bundle.unparcel(Bundle.java:249)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.os.Bundle.getParcelableArrayList(Bundle.java:1250)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.content.Intent.getParcelableArrayListExtra(Intent.java:4729)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at com.AMOV.mr.fit.ListaExerciciosActivity.onCreate(ListaExerciciosActivity.java:27)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.Activity.performCreate(Activity.java:5231)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-03 18:24:08.889: E/AndroidRuntime(18813):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
4
  • 1
    The error tells you what you are missing: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.AMOV.mr.fit.GrupoMuscular Commented Jan 3, 2015 at 18:38
  • 1
    Your stacktrace tells you exactly what's wrong - Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.AMOV.mr.fit.GrupoMuscular Commented Jan 3, 2015 at 18:38
  • @MikeM. you're right, but i don't know how i can apply it to my project Commented Jan 3, 2015 at 18:48
  • 2
    stackoverflow.com/questions/1626667/… Commented Jan 3, 2015 at 18:50

2 Answers 2

1

Your class missing CREATOR:

Class implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface.

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

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

Hope this help!

Sign up to request clarification or add additional context in comments.

1 Comment

That's what i missed! Thank's @Xingchen, it helped a lot!
1

You haven't make Your class Parcerable properly - Your writeToParcel method is empty, and You haven't create CREATOR object. Really simple example of implementing Parcerable interface is described on Android Developers:

http://developer.android.com/reference/android/os/Parcelable.html

The easier way to send object of custom class to another activity is using Serializable interface - Your class has only to implement it and whole work is done.

intent = new Intent(ExerciciosActivity.this, ListaExerciciosActivity.class);
Bundle bundle = new Bundle();  
bundle.putSerializable("gruposMusculares", gruposMusculares);
intent.putExtras(bundle);
startActivity(intent);

Notice that sending objects by Serializable is a little bit slower than by Parcerable and that all objects classes contained in class implementing Serializable have to implement Serializable too.

1 Comment

i had no idea that i could to the same using this method! Thank's!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.