Parcelable遇到IOException写入可序列化对象?

时间:2014-06-12 17:19:36

标签: java android serialization

我得到" Parcelable遇到IOException写入可序列化对象"此代码出错

public void onClick(View v) {
                tvResult.setText("Povezivanje s bazom u tijeku...");
                Intent i = new Intent("android.intent.action.MAINACTIVITY");

                Details details = new Details();
                details.host=etHost.getText().toString();
                details.user=etUsername.getText().toString();
                details.pass=etPass.getText().toString();
                details.database=etBaza.getText().toString();

                new GetData(tvResult).execute("");

                Bundle bundle = new Bundle();
                bundle.putSerializable("Detalji", details);
                i.putExtras(bundle);
                startActivity(i);

            }

这也是我的详细信息类:

public class Details implements Serializable 
    {

        private static final long serialVersionUID = 1L;
        private String host;
        private String pass;
        private String user;
        private String database;
    }

startActivity(i)命令之前一切正常,任何人都知道为什么?

2 个答案:

答案 0 :(得分:2)

在Details类中,您是否覆盖了writeToParcel和readFromParcel?

@Override
    public void writeToParcel(Parcel dest, int flags) {
}

如果要通过意图传递复杂对象,则需要实现Parceleable。

详细信息类需要在其自己的文件中,您没有实现parcelable。

答案 1 :(得分:1)

您必须对数据类Parcelable实施Details

public class Details implements Serializable, Parcelable {
    private static final long serialVersionUID = 1L;
    private String host;
    private String pass;
    private String user;
    private String database;

    public Details(Parcel in){
       this.host = in.readString();
       this.pass = in.readString();
       this.user = in.readString();
       this.database = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.host);
        dest.writeString(this.pass);
        dest.writeString(this.user);
        dest.writeString(this.database);
    }

    public static final Parcelable.Creator<Details> CREATOR = new Parcelable.Creator<Details>() {
        public Details createFromParcel(Parcel in) {
            return new Details(in); 
        }

        public Details[] newArray(int size) {
            return new Details[size];
        }
    };
}

现在我试着写一些适合你的东西,但我不是百分之百确定会这样做,所以从这里做一些附带的阅读: