I need help with an refresh button in my action bar. I already have this button but I want to let it rotate onclick. I've wrote this code here but I'm getting an error and the app is crashing:
The error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Mx XML:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:compat="http://schemas.android.com/tools"> <item android:id="@+id/menu_reloadbutton" android:icon="@drawable/ic_menu_reloadentry" android:title="@string/menu_reloadentry" app:showAsAction="ifRoom"/> </menu> My code:
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_training, menu); // Declare ImageView and Animation for rotation animation MenuItem reloadButton = (MenuItem) menu.findItem(R.id.menu_reloadbutton); final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_rotate); if (reloadButton != null) { //reloadButton.setImageResource(R.drawable.ic_menu_reloadentry); reloadButton.getActionView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { rotation.setRepeatCount(Animation.INFINITE); view.startAnimation(rotation); } }); } }