0

So I am making an android app that implements some custom classes. I want to create an object of class Menu_Organizer to other activities, but after I inizialize the object and send it to the next Activity the object is NULL. Here are the classes:

Menu Items class

import java.io.Serializable;

public class Menu_Items implements Serializable {

   private String Name = "";
   private String Description = "";
   private float Price = 0.0f;
   private String Category = "";

    /* Getters and Setters*/    
}

Menu Organizer class:

public  class Menu_Organizer implements Serializable {

    ArrayList<String> Categories;
    ArrayList<Menu_Items> Food;

// EDITED
public Menu_Organizer() {
    Categories = new ArrayList<String>();
    Food = new ArrayList<Menu_Items>();
}

    /* Other class methods */
}

First Activity (main)

public class MainActivity extends AppCompatActivity {
    private Button btn;
    public  Menu_Organizer menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onclick();
    }

    public void onclick() {
        btn = (Button) findViewById(R.id. btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Menu_Organizer menu = new Menu_Organizer();
                menu.New_Menu_Item("Carne", "Pollo", "Pollo Asado Rico!", 4.55f);

                Intent activity2= new Intent(MainActivity.this,temp.class);
                activity2.putExtra("Menu", menu);
                startActivity(activity2);
            }
        });
    }
}

Second Activity

public class temp extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_Second);
        Bundle bundle = getIntent().getExtras();

        Menu_Organizer menu = (Menu_Organizer)   bundle.getSerializable("Menu");
        String str= menu.Food.get(0).getName();
    }
}
Leoking938
  • 71
  • 1
  • 2
  • 11
  • 2
    The `Intent` in `MainActivity` is opening the `temp` `Activity`, but the other `Activity` class you've posted is `Second`. – Mike M. May 16 '16 at 22:20
  • You should show your implementation of New_Menu_Item. My guess is that your aren't creating a new ArrayList instance of Food before you add the items into it. – Jason Grife May 16 '16 at 23:17
  • http://stackoverflow.com/questions/4233873/how-do-i-get-extra-data-from-intent-on-android tried this instead of `Bundle bundle = getIntent().getExtras();` ? – iCantSeeSharp May 17 '16 at 02:12

2 Answers2

3

Alright, i think that the issue is, that when you pass your class object in a key value pair, you do it in an Intent object, but when you resolve your intent, you do that via a bundle object. So, in you temp Activity, you should resolve the intent like:

Intent intent = this.getIntent();
Menu_Organizer menu = (Menu_Organizer) intent.getSerializableExtra("Menu");

Try this, this should work. If you want to do it via a bundle, then create a bundle object first, then put whatever you want in that bundle. Add the bundle to your intent, and then resolve the bundle in your next Activity.

Just a tip, Class names generally do not contain _ in them, use CamelCase naming convention for all classes.

vibhor_shri
  • 374
  • 2
  • 12
0

I would recommend using EventBus library for this kind of thing. It is quite easy to use and gives you exactly this: sending and receiving custom object classes from one place to another (Fragments, Activities, Services, whatever you wish can send and receive objects).

I personally don't like intents cause they have too many limitations.

Vucko
  • 7,371
  • 2
  • 27
  • 45