Android如何将Object User从活动传递给片段

时间:2014-10-07 21:17:29

标签: android

Intent mIntent = new Intent(Login.this, PlatformActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("user",user);
                mIntent.putExtras(bundle);
                startActivity(mIntent);

public class User implements Serializable {

    private List<UserAccount> userAccountList;
    ...
    ...
}

RuntimeException:Parcelable遇到IOException,编写可序列化对象(name = com.orbis.mobile.User)

用户不可序列化,我想知道如何使用arrayList传递Object。如果用户没有设置userAccountList,则可以使用它。

2 个答案:

答案 0 :(得分:1)

            ArrayList<UserAccount> userAccountList = user.getUserAccountList();
            user.setUserAccountList(null);

            bundle.putSerializable("user", user);

            bundle.putSerializable("accountList", userAccountList);

            mIntent.putExtras(bundle);
            startActivity(mIntent);


    user = (User) getIntent().getSerializableExtra("user");

    ArrayList<UserAccount> accountNumberList = (ArrayList<UserAccount>) getIntent().getSerializableExtra("accountList");

    user.setUserAccountList(accountNumberList);

答案 1 :(得分:0)

List不是Serializable界面,您需要使用ArrayList等实现

请参阅 http://docs.oracle.com/javase/7/docs/api/java/util/List.html

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

public class User implements Serializable {

    private ArrayList<UserAccount> userAccountList;
    ...
    ...
}

同样UserAccount需要implements Serializable