我已经实现了一个 Parcelable 类:
public class Evento implements Parcelable {
private int;
private String ;
private String imagen;
//more atts
//Constructors, setters and getters
public Evento(Parcel in) {
this.id = in.readInt();
this.titulo = in.readString();
this.imagen=in.readString();
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(titulo);
dest.writeString(imagen);
}
public static final Parcelable.Creator<Evento> CREATOR = new Parcelable.Creator<Evento>() {
@Override
public Evento createFromParcel(Parcel in) {
return new Evento(in);
}
@Override
public Evento[] newArray(int size) {
return new Evento[size];
}
};
在我的活动中,我试图将 arrayList 传递给其他活动:我覆盖了 onItemClick 并且我以这种方式传递信息:
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//create new activitie
ArrayList<Eventos> eventos = new ArrayList<Eventos>();
Intent intent = new Intent(QueActivity.this, ProductosCategorias.class);
//Create information
Bundle informacion= new Bundle();
informacion.putParcelableArrayList("eventos", eventos);
intent.putExtras(informacion);
并在其他活动中接收它:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_productos_categorias);
lv = (ListView) findViewById(R.id.listaCategoriaElegida);
Bundle informacionRecibida = getIntent().getExtras();
ArrayList<Evento> eventos =new ArrayList<Evento>();
eventos= informacionRecibida.getParcelableArrayList("eventos");
我得到一个空指针异常。我已经对其进行了调试,当我执行 getParcelableArrayList("eventos") 时我得到了 NULL,我的意思是,我的 arraylist eventos 为空,所以,我没有收到任何信息。
任何帮助都会很棒