0

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.

AndroidDev
  • 20,466
  • 42
  • 148
  • 239

2 Answers2

0

According to the error, lvSelectedVenues.getChildAt(i) is returing null. This is probably because the child view you are trying to get is not currently visible, therefore returning null.

Check out these questions, their answers might help you: Question 1, Question 2

You can use RecyclerView as an alternative and it already includes animations. Here is an example of that.

Community
  • 1
  • 1
TychoTheTaco
  • 664
  • 1
  • 7
  • 28
0

I strongly advice you to move to RecylerView which has default ItemAnimator that handles different types of animations per-se insert/delete/update - it is far more efficient as well. Once you get to know how it functions it is a lot easier to implement.

How to animate RecyclerView items when they appear

Community
  • 1
  • 1
takrishna
  • 4,884
  • 3
  • 18
  • 35