Android – 将对象传递给另一个活动

我正在使用跟随类,我有一个对象: http : //pastebin.com/rKmtbDgF

我试图通过使用:

Intent booklist = new Intent(getBaseContext(), BookList.class); Bundle bundle = new Bundle(); bundle.putParcelable("imageManager", (Parcelable) im); booklist.putExtras(bundle); booklist.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(booklist); 

我试图通过以下方式接收它:

 ImageManager im = (ImageManager) getIntent().getExtras().getParcelable("imageManager"); 

我收到以下错误:

 java.lang.ClassCastException: com.example.example.ImageManager 

最简单的方法是实现Serializeable ..

 import java.io.Serializable; @SuppressWarnings("serial") public class Student implements Serializable { public Student(int age, String name){ this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } private int age; private String name; } 

将对象从活动A发送到活动B.

 Student student = new Student (18,"Zar E Ahmer"); Intent i = new Intent(this, B.class); i.putExtra("studentObject", student); startActivity(i); 

在活动B中获取对象

 Intent i = getIntent(); Student student = (Student)i.getSerializableExtra("studentObject"); 

转到这里 ,粘贴你的类结构。它将为你创建parcelable类,然后你可以很容易地围绕活动传递你的对象。

试试以下post。

如何使我的自定义对象Parcelable?

问题是ImageManager不是可比对象。 所以这给出了施法错误。 如果imageManager是你的,那么你应该让类实现Pracabale,就像sais上面的url一样。

编辑:

上面应该是做Parceables的正确方法,但在你的情况下,我真的会说你不应该在不同的活动之间传递ImageManager。 因为您在ImageManager类中使用的上下文将被处理..并且您肯定会收到错误。

那么为什么不改为创建类的新实例,只将Bitmap传递给它(在转发位图和其他不与上下文相关的信息之后关闭它。)