Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Android, passing data into new Activity (bundle)

Hi I am using the self-destructing message tutorial do create an app of my own.

In my app, I want the users to see a list of cities, select the city, then see people associate with that city.

So, I have this to get the data:

protected void onResume() {
    super.onResume();

    setProgressBarIndeterminateVisibility(true);


    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.orderByAscending(ParseConstants.KEY_LOCATION);
    query.setLimit(1000);
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> users, ParseException e) {
            setProgressBarIndeterminateVisibility(false);

            if (e == null) {
                // Success
                mMidwifeLocation = users;
                String[] locations = new String[mMidwifeLocation.size()];
                String check;
                int i = 0;
                for(ParseUser user : mMidwifeLocation) {
                    check=user.getString("city");
                    if(check!=null){
                        if(!Arrays.asList(locations).contains(check)){
                            locations[i] = user.getString("city");
                        }
                    }
                    i++;
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        SearchingMidwife.this,
                        android.R.layout.simple_list_item_checked,
                        locations);
                setListAdapter(adapter);
            }
            else {
                Log.e(TAG, e.getMessage());
                AlertDialog.Builder builder = new AlertDialog.Builder(SearchingMidwife.this);
                builder.setMessage(e.getMessage())
                        .setTitle(R.string.error_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        }
    });

And this to pass the selected item to the new activity:

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

    SparseBooleanArray checked = l.getCheckedItemPositions();

    ArrayList<String> selectedItems = new ArrayList<String>();

    for (int i = 0; i < checked.size(); i++) {
        // Item position in adapter
        position = checked.keyAt(i);
        // Add sport if it is checked i.e.) == TRUE!
        if (checked.valueAt(i))
            selectedItems.add(adapter.getItem(position));
    }

    String[] outputStrArr = new String[selectedItems.size()];

    for (int i = 0; i < selectedItems.size(); i++) {
        outputStrArr[i] = selectedItems.get(i);
    }

    Intent intent = new Intent(getApplicationContext(),
            MidwifeResultList.class);

    // Create a bundle object
    Bundle b = new Bundle();
    b.putStringArray("selectedItems", outputStrArr);

    // Add the bundle to the intent.
    intent.putExtras(b);

    // start the ResultActivity
    startActivity(intent);


}

When I try it though, the app stops with an error once I select the item...

FATAL EXCEPTION: main Process: android.bignerdranch.com.mobilemidwife, PID: 31604 java.lang.NullPointerException at android.bignerdranch.com.mobilemidwife.SearchingMidwife.onListItemClick(SearchingMidwife.java:169)

Which points to this:

if (checked.valueAt(i)) selectedItems.add(adapter.getItem(position)); }

I declare adapter before onCreate:

ArrayAdapter<String> adapter;

I assume this is the same adapter that is used in OnResume...something is not quite right, not sure what that would be.

Thanks so much for your help/insights

Michael Cabus

2 Answers

Nicolas Hampton
Nicolas Hampton
44,725 Points

Michael, if this is all in one class, which I think it is, then your declaration for the adapter is inside of a if loop inside of an overriden done method inside of a findinbackground query. That's outside the scope of your onListClickItem method, so when you reach that line, the adapter can't be found, resulting in a null exception. I would suggest at least declaring it at the top of this class, maybe with your member variables, to get the scope you need. I'm not as far as you but I've taken some time on a project for practice and ran across ArrayAdapters for ListViews, they can be tricky, and that was part of my problem. Hope that helps!

Nicolas

Nicolas Hampton
Nicolas Hampton
44,725 Points

Also, something that will have a small effect on your runtime is that your int i declaration to count through for(ParseUser user : mMidwifeLocation) is a memory leak. i is outside of the for loop and isn't reclaimed after the loop. I would suggest finding a way to put it inside the loop so that the computer reclaims that variable and value at the end of the loop cycle.