我试图在片段隐藏之前设置过渡动画,但是我得到了nullpointerexception。没有.setCustomAnimation方法,代码可以正常运行并且片段被隐藏,空指针从何而来?
public class MainActivity extends AppCompatActivity {
private TextView mavisTxt;
private Button testBtn;
private HeaderNav navigationBar;
private FragmentManager fm;
private FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initVariables();
}
private void initVariables() {
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
navigationBar = (HeaderNav)fm.findFragmentById(R.id.navigationBar);
mavisTxt = (TextView)findViewById(R.id.mavisTxt);
testBtn = (Button)findViewById(R.id.testBtn);
testBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.hide(navigationBar)
.commit();
if(navigationBar.isHidden()) {mavisTxt.setText("navbar is hidden");}
}
});
}
}
这是我得到的日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.startViewTransition(android.view.View)' on a null object reference
答案 0 :(得分:0)
然后查看是否有与此相关的内容以及这3行
navigationBar = (HeaderNav)fm.findFragmentById(R.id.navigationBar);
mavisTxt = (TextView)findViewById(R.id.mavisTxt);
testBtn = (Button)findViewById(R.id.testBtn);