creating bundle and sending over to new activity

2019-04-25 07:55发布

问题:

I"m creating a bundle in one activity, then extracting it in another activity

here is when it's created in he main activity

//Create bundle to reference values in next class
                Bundle bundle = new Bundle();
                bundle.putInt("ODD", odd);
                bundle.putInt("EVEN", even);
                bundle.putInt("SMALL", small);
                bundle.putInt("BIG", big);
                //After all data has been entered and calculated, go to new page for results
                Intent myIntent = new Intent();
                myIntent.setClass(getBaseContext(), Results.class);
                startActivity(myIntent);
                //Add the bundle into myIntent for referencing variables
                myIntent.putExtras(bundle);

Then when I extract on the other activity

//Extract the bundle from the intent to use variables
    Bundle bundle = getIntent().getExtras();
    //Extract each value from the bundle for usage
    int odd = bundle.getInt("ODD");
    int even = bundle.getInt("EVEN");
    int big = bundle.getInt("BIG");
    int small = bundle.getInt("SMALL");

The application crashes when I'm extracting the bundle on the second activity. But when I comment out the extraction of the bundle. The app runs fine. So I've narrowed it down to that.

My log cat doesn't really explain what the error is, or i just dont understand it

ideas?

回答1:

You are adding below code after calling startActivity(myIntent);

//Add the bundle into myIntent for referencing variables
                myIntent.putExtras(bundle);

Put this just before startActivity(myIntent);