1

我正在尝试将自定义对象从一个活动发送到另一个活动,但是当我调用启动活动时它崩溃了。

下面是我使用的片段。

我的活动实现Serializable

ArrayList<CUSTOM_OBJECT> Cus_Obje_arraylist = new ArrayList<CUSTOM_OBJECT>();

这是我的意图:

Intent inte = new Intent(getApplicationContext(), ListActivity.class); `
inte.putExtra("list",Cus_Obje_arraylist);`
startActivity(inte);

请让我知道它为什么会崩溃或我可以使用什么替代方式?

4

2 回答 2

7

I can give a suggestion. I do this in my project.

1.Implement a singleton class as the bridge to pass object. (Hopefully you know what's singleton, I you don't, add comment to tell me.

class BridgeClass {
    private BridgeClass() {}

    static BridgeClass obj = nil;
    public BridgeClass instance() {
         if (obj == nil) obj = new BridgeClass();
         return obj;
    }

    public ArrayList<CUSTOM_OBJECT> cache;
 }

2.In the from activity,

BridgeClass.instance().cache = Cus_Obje_arraylist;

3.Then in the to activity, you can get it from the bridge class.

ArrayList<CUSTOM_OBJECT> Cus_Obje_arraylist = BridgeClass.instance().cache;
于 2012-11-30T01:27:36.877 回答
0

You need to create the Parcelable object to pass the custom array list from one activity to the another actvity.

Then put it into the Bundle object using the this api.

putParcelableArrayList(key, value);
getParcelableArrayList(key);

=== Sender ===

ArrayList<Custom> ar = new ArrayList<Custom>();
Bundle bundle = new Bundle("test");

bundle.putParcelableArrayList("key", ar);
Intent intent = new Intent(this, anotherActivity.class);
intent.putBundle(bundle);

=== Receiver ===

Bundle bundle = getIntent().getBundleExtra("test");
ArrayList<Custom> ar = bundle.getParcelableArrayList("key");

If you have any question, comment it.

于 2012-11-30T01:39:09.840 回答