1

I am trying to set a transition animation on a fragment before it hides, but I am getting a nullpointerexception. Without the .setCustomAnimation method, the code runs fine and the fragment hides, where is the nullpointer coming from?

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");}
            }
        });

    }
}

Here is the log I am getting :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.startViewTransition(android.view.View)' on a null object reference
Chain Cross
  • 351
  • 1
  • 2
  • 12

1 Answers1

0

Take a look in this post: Attempt to invoke virtual method 'android.view.View android.view.View.getRootView()' on a null object reference

And see if there any reference to that and this 3 lines

navigationBar = (HeaderNav)fm.findFragmentById(R.id.navigationBar);
mavisTxt = (TextView)findViewById(R.id.mavisTxt);
testBtn = (Button)findViewById(R.id.testBtn);
RodolfoTVA
  • 77
  • 9