0

I have an activity that allows the user to start a second activity. The second activity has a list of items which I add to an array list. When I return to the previous activity I want to display the size of the array list.

However I am having a problem with onResume(). It is called when my first activity is created and as a result generates an error as the array list does not exist when it is first launched!

onResume():

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    getIntentData();
    calcSubTotal(orderData);
}

getIntentData():

public void getIntentData(){
    b = new Bundle();
    b = getIntent().getExtras();
    orderData = b.getParcelable("order");
    Toast.makeText(this.getApplicationContext(), orderData.size(), Toast.LENGTH_LONG).show();
}

onCreate() of second activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starters);
    createTestData();

    b = new Bundle();
    orderData = new MenuItemList();

    adapter = new MenuItemArrayAdapter(this, starters);
    this.setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    Toast.makeText(this.getApplicationContext(), l.getItemAtPosition(position).toString() + " clicked", Toast.LENGTH_LONG).show();
    //add clicked item to orderData....
    MenuItem m = (MenuItem)l.getItemAtPosition(position);
    //create new item
    orderData.add(m);   
}

Any idea how I might be able to control this?

ERROR:

java.lang.RuntimeException: Unable to resume activity {com.example.waitronproto3/com.example.waitronproto3.SectionsActivity}: java.lang.NullPointerException

Javacadabra
  • 5,578
  • 15
  • 84
  • 152

3 Answers3

2

I think you may want to have a look at startActivityForResult instead, when you're starting your second Activity. It'll allow your second activity to return a result back to your first activity. You can read up on it at the Activity documentation, specifically the "Starting Activities and Getting Results" section of the document.

Edit: By the looks of your code - nothing you're doing is either storing a bundle from the second activity and sending it back to the first. So you'll never get the proper Bundle data in your first activity. As suggested, look into startActivityForResult to launch your second activity with. This will allow you to return data back into your first activity with ease.

jlindenbaum
  • 1,891
  • 18
  • 28
  • I actually tried this by creating a static final variable and passing that with the startActivityForResult() method. I then overrided onActivityResult() but for some reason It was not getting called when the activity resumed... – Javacadabra Dec 11 '12 at 21:39
  • No, that won't work. In Activity A launch your Activity B with startActivityForResult. In Activity B, when you've done all your work, use the setResult(int, Intent) function to return a result code and Intent with data back to your Activity A. When you use that function before your activity is closed (or close it manually with finish()) then you'll get the proper intent with data in onActivityResult(). – jlindenbaum Dec 11 '12 at 21:47
  • Ok, I made the changes and I was getting this error when I try to retrieve the data in the Intent: java.lang.RuntimeException: Unable to resume activity: java.lang.NullPointerException. However if I remove the lines of code where I pull the data from the intent it successfully displays a toast, So its working. Any idea what might be causing the nullpointer?? – Javacadabra Dec 11 '12 at 21:58
  • 1
    You're accessing a null value. :P You should check that the data coming out of the intent is not null, and that the key you're trying to get from the bundle is actually there. Make sure your key names are the same in both activities. – jlindenbaum Dec 11 '12 at 22:20
  • ok cool, I'll give that a shot! I'm gonna try and get some other functionality put in before I try again. Will let you know if it works !thanks for the help. – Javacadabra Dec 11 '12 at 22:58
0

However I am having a problem with onResume(). It is called when my first activity is created and as a result generates an error as the array list does not exist when it is first launched!

I recommend changing getIntentData() to check if the appropriate data exists first:

public void getIntentData(){
    Intent intent = getIntent();
    if(intent != null && intent.hasExtra("order")) {
        orderData = b.getParcelable("order");
        Toast.makeText(this.getApplicationContext(), orderData.size(), Toast.LENGTH_LONG).show();
        calculateSubTotal(order);
    }
}

And update onResume():

@Override
protected void onResume() {
    super.onResume();
    getIntentData();
}

(Though you could simply put getIntentData() in onResume() now.)

Sam
  • 86,580
  • 20
  • 181
  • 179
0

Your onResume() will be called after onCreate() according to the Android Lifecycle so you will want to check that the data is not null before trying to use it.

`if(intentData != null)

//do something`
codeMagic
  • 44,549
  • 13
  • 77
  • 93