-2

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
Goku
  • 8,341
  • 7
  • 43
  • 73
Fresco
  • 333
  • 3
  • 12

4 Answers4

1

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());
    }
Goku
  • 8,341
  • 7
  • 43
  • 73
1
public void onDestroy() {
        super.onDestroy();
       if(appbars!=null){  // use this 
     appbars.animate().translationY(0).alpha(1).setDuration(100).setInterpolator(new DecelerateInterpolator());   

    }
Goku
  • 8,341
  • 7
  • 43
  • 73
oj24031989
  • 31
  • 1
  • 4
1

Your appbars was destroyed and no more exist. Also you need to move stop work in onPause() for better performance.

Activity lifecycle :

enter image description here

Leonid Veremchuk
  • 1,913
  • 14
  • 26
  • As fellow members suggested: problem was `onDestroy` triggered before the `AppBarLayout` loaded thus I got the null pointer error – Fresco Dec 29 '17 at 06:58
  • I was tell you about bad practice in your code. If you want to get answer how avoid this you had to many examples below. GL and HF – Leonid Veremchuk Dec 29 '17 at 07:13
  • If i don't use `onDestory` I have to implement this code in other tabs as well. Thanks for your suggestion – Fresco Dec 29 '17 at 07:29
1

try with this..

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

Hope this helps..

Nikhil Borad
  • 2,055
  • 15
  • 20