简体   繁体   中英

Parcelable encountered IOException writing serializable object?

I get "Parcelable encountered IOException writing serializable object" error on this code

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);

            }

Here is also my Details class:

public class Details implements Serializable 
    {

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

Everything is working good till startActivity(i) command, anyone have idea why?

in Details class have you overridden writeToParcel and readFromParcel?

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

Details needs to implement Parceleable if you want to pass complex objects via intents.

Details class needs to be in it's own file you are not implementing parcelable.

You have to implement Parcelable to your data class 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];
        }
    };
}

Now I tried to write something what should work for you, but I am not 100% sure it will, so do some aditional reading from here:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM