Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a crash on tablet that is quite misterious to me :

java.lang.NullPointerException: Attempt to read from field 'int android.support.v4.app.Fragment.mContainerId' on a null object reference                                                                          
   at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1012)                                                                          
   at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:971)                                                                          
   at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)                                                                          
   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2143)                                                                          
   at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)                                                                          
   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)                                                                          
   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)                                                                          
   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:6077)                                                                          
   at java.lang.reflect.Method.invoke(Native Method)                                                                          
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                          
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

It only happens on my api 24 Pixel C. Debug mode couldn't help me to find out where. It happends when I clic a button that removes a fragment, but the "fragment removing" part of code gets executed correctly, something happends after execution of my OnClickListener.

I removed transaction animations and got the same crash.

Has anybody experienced that before ?

Thanks

@RoShanShan It was indeed the way that I did remove the fragment, I updated my original post with the solution – Renaud Favier Feb 23, 2017 at 16:01

Seems that the solution were not to keep a reference of the fragment but find it with tag :

Before :

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
courseLauncherFragment = CourseLauncherFragment.newInstance(courseId);
fragmentTransaction.replace(R.id.launcher_finish_frame_layout, courseLauncherFragment);
fragmentTransaction.commit();
getSupportFragmentManager().executePendingTransactions();

then to remove it :

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
fragmentTransaction.remove(courseLauncherFragment);
fragmentTransaction.commit();

And now :

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.launcher_finish_frame_layout, CourseLauncherFragment.newInstance(courseId), LAUNCHER_FRAGMENT_TAG);
fragmentTransaction.commit();
getSupportFragmentManager().executePendingTransactions();

then :

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
Fragment fragment = fragmentManager.findFragmentByTag(LAUNCHER_FRAGMENT_TAG);
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.