I have an activity that contains two listviews. When I click on one of the lists, the item clicked is removed from that list and added to the other list. I'm trying to provide some animations on this process.
I can successfully animate the removal of the existing item in the initial list. However, I cannot yet animate the insertion of the new item into the other list. I get an NPE error when I try to start the animation on the new list item.
lvAvailableVenues.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Venue venue = availableVenues.get(position);
availableVenues.remove(position);
availableVenuesAdapter.notifyDataSetChanged();
slideOutRight(position);
lvSelectedVenues.scheduleLayoutAnimation();
selectedVenues.add(availableVenues.get(position));
Collections.sort(selectedVenues, new Venue.VenueComparator());
selectedVenuesAdapter.notifyDataSetChanged();
slideInLeft(venue.getVenueId());
}
});
private void slideOutRight(int position) {
Animation anim = AnimationUtils.loadAnimation(VenueFilterActivity.this, android.R.anim.slide_out_right);
anim.setDuration(500);
lvAvailableVenues.getChildAt(position).startAnimation(anim );
}
private void slideInLeft(int venueId) {
for (int i=0; i<selectedVenues.size(); i++) {
if (selectedVenues.get(i).getVenueId() == venueId) {
Animation anim = AnimationUtils.loadAnimation(VenueFilterActivity.this, android.R.anim.slide_in_left);
anim.setDuration(500);
// this is the line that crashes!!!
lvSelectedVenues.getChildAt(i).startAnimation(anim);
break;
}
}
}
Here is my error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.startAnimation(android.view.animation.Animation)' on a null object reference
Note that if I comment out the offending line, then my list is updated correctly, but without the animation that I am trying to create.