1

I have a ViewPager where one of its belonging fragments has its own ActionBar item, so that when you slide to that page the item comes forth in the ActionBar, slide to another page the item goes away.

I would like this to happen with a fade-in/fade-out animation - but don't know how.

I've tried the following. But it gives me a NullPointerExecption on itemView.startAnimation(fade_in);

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.intruders_list, menu); 

        // Setup animation
        Animation fade_in = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
        fade_in.setInterpolator(new AccelerateInterpolator());
        fade_in.setDuration(250); 

        // Animate  
        MenuItem deleteItem = menu.findItem(R.id.action_delete);
        View itemView = deleteItem.getActionView();
        itemView.startAnimation(fade_in); // NPE HERE

        super.onCreateOptionsMenu(menu, inflater);  
}
Jakob Harteg
  • 9,587
  • 15
  • 56
  • 78

1 Answers1

2

You are getting a NullPointerException because you are trying to get an action view where none is set. getActionView() is returning null in you case.

To solve this issue you need to set one with deleteItem.setActionView(R.layout.layout_action_view);.
Alternatively you can set it with android:actionLayout="@layout/layout_action_view" on your item in intruders_list.xml.

layout_action_view.xml could look as simple as this:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
   style="?android:attr/actionButtonStyle"
   android:id="@+id/iv_action"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@android:drawable/ic_menu_rotate" />
msal
  • 947
  • 1
  • 9
  • 30