1

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!

mcCat
  • 120
  • 2
  • 13
  • You can't serialize an `Activity`, or any subclass thereof. Does your `Controller` class really need to extend `AppCompatActivity`? – Mike M. Jul 01 '16 at 00:06
  • 1
    Oh, okay that makes a lot of sense. Actually, I don't think it needs to :) Thank you so much I'm gonna try that right now. – mcCat Jul 01 '16 at 00:14
  • Unfortunately, I still have the same error :/ – mcCat Jul 01 '16 at 00:15
  • Well, I'm guessing that you've got some non-`Serializable` member in `Controller`, but we'll need to see the class and stack trace to be certain what the issue is. – Mike M. Jul 01 '16 at 00:17
  • Yep, I edited my post, it has the complete Controller class now. – mcCat Jul 01 '16 at 00:22
  • Possible duplicate of [Passing JSONObject into another activity](http://stackoverflow.com/questions/5082122/passing-jsonobject-into-another-activity) – Creos Jul 01 '16 at 00:30
  • Every member in `Controller` that you serialize must itself be `Serializable`. I don't know what any of those classes are, but if they're not `Serializable`, you can't serialize them. – Mike M. Jul 01 '16 at 00:47
  • Most of them extend Activity, so I guess they are NOT serializable. – mcCat Jul 01 '16 at 00:48
  • So in this case, is there any other way I could pass my controller? – mcCat Jul 01 '16 at 00:49
  • Are your nested members also serializeable? – Submersed Jul 01 '16 at 00:50
  • It sounds like you probably don't want to do what you're trying to do. There's really no reason for an `Activity` to have a reference to another `Activity`. There's surely some other way to accomplish what you want to do. – Mike M. Jul 01 '16 at 00:51
  • Could you point me in the right direction? :) – mcCat Jul 01 '16 at 01:00
  • Not really, 'cause I don't know what you're doing. There are several different ways to share objects/data between Activities. Your first step should be to figure out what the absolute minimum is that you need to pass. If you can boil it down to basic `Serializable` or `Parcelable` objects, then you can stick with what you're doing, and just change your `Controller` class. – Mike M. Jul 01 '16 at 01:36
  • Okay I understand! Basically what I wanna do: my ButtonLayout has a xml layout that holds a button that is in several other layouts, and the buttonClass needs to know which layout/activity is open. Basically the button triggers methods in different activities, and it needs to know which one to use. So I tried to use the Controller to provide that information. I hope I was able to explain what I'm tryong to do :) thanks for all your help! – mcCat Jul 01 '16 at 10:13
  • Anyone? :/ I just need a hint, keyword, something to look for, I don't need a complete solution to my problem, just an idea so I know what to look for. – mcCat Jul 02 '16 at 09:43

1 Answers1

2

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"));
Kabir
  • 852
  • 7
  • 11
Victor Hugo Montes
  • 1,270
  • 1
  • 17
  • 28