First things first, I’m using the compatibility library and here’s the error I’m getting:
11-22 19:57:09.111: ERROR/AndroidRuntime(28867): FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v4.app.BackStackRecord.bumpBackStackNesting(BackStackRecord.java:518)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:563)
at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1379)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:419)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
This error only occurs when the application is in PORTRAIT orientation and I’m attempting to use a FragmentTransaction to hide two other fragments and show a new one. This code gets executed when a user presses a button in the app.
Here’s the code that throws (as soon as the commit is called and gets executed the error above is being thrown).
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.hide(fragmentA);
transaction.hide(fragmentB);
transaction.replace(R.id.fragment_container, new PurchaseFragment());
transaction.commit();
Any ideas why this might be happening?
Figured out what it was. I’m using Roboguice to inject some components and due to some logic not shown above, the fragments were null. If any fragment is null the
transaction.hide(fragment)then the above exception will occur.After I fixed the logic error, the fragments were no longer null. At that point the app worked as expected.