NullPointerException onDestory [复制]

问题描述 投票:-2回答:4

这个问题在这里已有答案:

当应用程序启动时我尝试立即旋转屏幕它给了我这个错误:

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

    }

错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator
 android.support.design.widget.AppBarLayout.animate()' on a null object
 reference
android android-appbarlayout
4个回答
1
投票

试试这个检查你的appbars是否为null

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

1
投票

试试这个......

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

希望这可以帮助..


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

    }

1
投票

您的appbars已被销毁,不再存在。此外,您需要在onPause()中移动停止工作以获得更好的性能。

活动生命周期:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.