Search code examples
androidandroid-appbarlayout

NullPointerException onDestory


When the app starts and I try to immediately rotate the screen It gives me this error:

public void onDestroy() {
        super.onDestroy();
        appbars.animate().translationY(0).alpha(1).setDuration(100).setInterpolator(new DecelerateInterpolator());

    }

Error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator
 android.support.design.widget.AppBarLayout.animate()' on a null object
 reference

Solution

  • Try this check your appbars is null or not like this

     public void onDestroy() {
         super.onDestroy();
           if(appbars!=null) {
           appbars.animate().translationY(0).alpha(1).setDuration(100).setInterpolator(new DecelerateInterpolator());
        }