7

I keep getting the setnextanim crash without a trace to the exact location in my code. Here's the only place I use nextanim :

@Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        Animation defaultAnimation = super.onCreateAnimation(transit, enter, nextAnim);

        if (defaultAnimation == null && nextAnim != 0) {
            defaultAnimation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
        }

        if (defaultAnimation != null) {
            if (getView() != null) {
                getView().setLayerType(View.LAYER_TYPE_HARDWARE, null);
            }

            defaultAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                public void onAnimationEnd(Animation animation) {
                    if (getView() != null) {
                        getView().setLayerType(View.LAYER_TYPE_NONE, null);
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }

        return defaultAnimation;
    }

and here's the trace I could find:

FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextAnim(int)' on a null object reference
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:765)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Any idea what I am doing wrong that might invoke this crash ?

eis
  • 51,991
  • 13
  • 150
  • 199
  • 1
    @zoe This is not about the null pointer exception but the fact that I cannot trace it back to the method that caused it. Does anything within my code look suspicious that can lead to this null pointer exception? –  Jan 04 '18 at 20:13
  • @Marissa Nicholas Your `Animation defaultAnimation = super.onCreateAnimation(transit, enter, nextAnim);` should look like this `Animation defaultAnimation;`. This is nice article for `Animation` method inside `Fragment`: http://en.proft.me/2017/07/29/customizing-transition-animations-between-activiti/ – Stanojkovic Jan 04 '18 at 21:09
  • what about initializing it : Animation defaultAnimation; , what value should i assign to initialize it? –  Jan 04 '18 at 22:02

4 Answers4

13

Looking at the exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextAnim(int)' on a null object reference

and the fact that the stack trace contains no references to your code, I think it is reasonable to assume it is not related to this part of your code.

Rather, it is possibly related to some view being garbage collected/nulled somewhere down the line and then trying to use it later, calling show/hide or similar on a null object. See this discussion.

eis
  • 51,991
  • 13
  • 150
  • 199
  • any idea about my question about uploading an image to server? : https://stackoverflow.com/questions/57598276/how-do-i-pass-a-multi-part-body-parameter-for-rxjava2-androidnetworking-post-req –  Aug 25 '19 at 16:53
12

In my case I removed a null fragment from stack:

val fragment = getSelectedFragment() // null
supportFragmentManager?.beginTransaction()
    ?.remove(fragment)
    ?.commit()
CoolMind
  • 26,736
  • 15
  • 188
  • 224
1

For me it was fragmentTransaction.hide(fragment) whereas my fragment was null.

Hope this will help anyone.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
1

For me it was:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(currentFragment);
ft.attach(currentFragment);
ft.commit();

where currentFragment was null.

So be sure that the fragment is not null when you try to use it.

vekerdyb
  • 1,213
  • 12
  • 26
Sebi
  • 133
  • 1
  • 9