mcCat
mcCat

Reputation: 120

Problems with passing serializable object between activities

I'm trying to pass an object to an activity. I know there's a million questions just like this one, and I'm sure somewhere out here is an answer, but I can't seem to find it and I just can't get this to work. I keep getting error messages (at first I kept getting NullPointerException, not it's java.lang.RuntimeException: Parcelable encountered IOException writing serializable object).

This is the method where I'm trying to pass it from:

public void createView(){
    Intent mIntent = new Intent(getMain(), ButtonLayout.class);
    mIntent.putExtra("controller", getController());
    getMain().startActivity(mIntent);
}

To here:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.button_panel);

    this.controller = (Controller) getIntent().getSerializableExtra("controller");

    calculate = (Button) findViewById(R.id.calc);
    calculate.setOnClickListener(new WorkAppOnClickListener(this.controller));
}

My Controller class implements Serializable:

import java.io.Serializable;

public class Controller implements Serializable{

    private Calculate currentActivity;
    private ButtonLayoutModel buttonLayout;
    AvModel av;

    public void setCurrentActivity(Calculate active){
        this.currentActivity = active;
    }

    public Calculate getCurrentActivity(){
        return this.currentActivity;
    }

    public void setAv(AvModel av){
        this.av = av;
    }

    public AvModel getAv(){
        return this.av;
    }

    public void setButtonLayout(ButtonLayoutModel bl){
        this.buttonLayout = bl;
    }

    public ButtonLayoutModel getButtonLayout(){
        return this.buttonLayout;
    }

}

I have tried casting to Serializable instead of Controller where I try to getSerializableExtra, but that does not seem help either ;(

Thanks for reading!

Upvotes: 1

Views: 497

Answers (1)

Victor Hugo Montes
Victor Hugo Montes

Reputation: 1322

It has been answered already on : Passing JSONObject into another activity

You can simply put an entire JSONObject as a string. Something like this:

i.putString("product", jsonObj.toString);

And then in the MovieProductActivity you could

JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("product"));

Upvotes: 2

Related Questions