Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad Parcelable exception

Tags:

java

android

first of all I must say I´ve been searching for an answer to my problem here at this site and I found several topics regarding this exception but after all the threads didn´t help me that much so that´s why I´m posting this.

I´m getting a Bad Parcelable exception everytime I try to recover information from a parcel. Here's my class code.

public class Persona implements Parcelable {

private String nombre,email,tel;

public Persona() {
    nombre = "";
    email = "";
    tel = "";
}

public Persona(String nombre, String email, String tel) {
    this.nombre = nombre;
    this.email = email;
    this.tel = tel;
}

public Persona(Parcel p){
    setNombre(p.readString());
    setEmail(p.readString());
    setTel(p.readString());
}

public String getNombre() {
    return nombre;
}

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

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getTel() {
    return tel;
}

public void setTel(String tel) {
    this.tel = tel;
}

@Override 
public String toString() {  
  return this.getNombre() + " " + this.getEmail() + " " + this.getTel();
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel des, int flags) {

    des.writeString(getNombre());
    des.writeString(getEmail());
    des.writeString(getTel());

}

public static final Parcelable.Creator<Persona> creator = new Creator<Persona>() {

    public Persona createFromParcel(Parcel source) {

        return new Persona(source);
    }

    public Persona[] newArray(int size) {

        return new Persona[size];
    }

};

}

And my activity code (this activity retrieves an intent that carries an ArrayList of the type I just specified)

public class Contacts extends Activity {

private ArrayAdapter<String> names;
private Intent intent;
private ArrayList<String>nameList;
private ListView list;

public Contacts() {
    intent = new Intent();
    nameList = new ArrayList<String>();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts);
    setIntent(getIntent());
    iterateContacts();
    names = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getNameList());
    getList().setAdapter(getNames());
}

public void iterateContacts(){

    ArrayList<Persona> aux = GetIntent().getParcelableArrayListExtra("contactList");

    for(int i = 0; i<aux.size(); i++){

        String name = aux.get(i).getNombre();
        getNameList().add(name);
    }
}

public ArrayAdapter<String> getNames() {
    return names;
}

public void setNames(ArrayAdapter<String> names) {
    this.names = names;
}

public Intent GetIntent() {
    return intent;
}

public void setIntent(Intent intent) {
    this.intent = intent;
}

public ArrayList<String> getNameList() {
    return nameList;
}

public void setNameList(ArrayList<String> nameList) {
    this.nameList = nameList;
}

public ListView getList() {
    return list;
}

public void setList(ListView list) {
    this.list = list;
}

}

LOGCAT:

E/AndroidRuntime(861): FATAL EXCEPTION: main
09-10 02:39:10.738: E/AndroidRuntime(861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.take2/com.take2.Contacts}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.take2.Persona
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.os.Looper.loop(Looper.java:137)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-10 02:39:10.738: E/AndroidRuntime(861):  at java.lang.reflect.Method.invokeNative(Native Method)
09-10 02:39:10.738: E/AndroidRuntime(861):  at java.lang.reflect.Method.invoke(Method.java:511)
09-10 02:39:10.738: E/AndroidRuntime(861):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-10 02:39:10.738: E/AndroidRuntime(861):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 02:39:10.738: E/AndroidRuntime(861):  at dalvik.system.NativeStart.main(Native Method)
09-10 02:39:10.738: E/AndroidRuntime(861):  at com.take2.Contacts.onCreate(Contacts.java:28)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.app.Activity.performCreate(Activity.java:5008)
09-10 02:39:10.738: E/AndroidRuntime(861):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
like image 707
user1659653 Avatar asked Sep 10 '12 09:09

user1659653


1 Answers

Change this:

public static final Parcelable.Creator<Persona> creator = new Creator<Persona>() {

To this:

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

Edit:

Also looks like your getList() returns null since it wasnt set before you do this at the end of onCreate:

getList().setAdapter(getNames());

Dont see were you initialize it, might this be the cause?

like image 148
Ostkontentitan Avatar answered Nov 17 '22 11:11

Ostkontentitan