FragmentManager NullPointerException while trying to commitAllowingStateLoss

Context: I have an Activity with Fragment and 3 InnerFragments . When Fragment onDestroy() is called, I want to remove the inner fragments from the FragmentManager . The code from onDestroy() below.

Problem: FragmentManager throws NullPointerException , possibly when commitAllowingStateLoss() called. I do not understand why.

 @Override public void onDestroy() { super.onDestroy(); if (getFragmentManager().findFragmentById(R.id.fragment_framelayout_left) != null) { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.remove(mLeftFragment); fragmentTransaction.commitAllowingStateLoss(); } } 

Stack trace:

 02-11 12:15:14.162: E/AndroidRuntime(25911): FATAL EXCEPTION: main 02-11 12:15:14.162: E/AndroidRuntime(25911): java.lang.NullPointerException 02-11 12:15:14.162: E/AndroidRuntime(25911): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1419) 02-11 12:15:14.162: E/AndroidRuntime(25911): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 02-11 12:15:14.162: E/AndroidRuntime(25911): at android.os.Handler.handleCallback(Handler.java:725) 02-11 12:15:14.162: E/AndroidRuntime(25911): at android.os.Handler.dispatchMessage(Handler.java:92) 02-11 12:15:14.162: E/AndroidRuntime(25911): at android.os.Looper.loop(Looper.java:137) 02-11 12:15:14.162: E/AndroidRuntime(25911): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-11 12:15:14.162: E/AndroidRuntime(25911): at java.lang.reflect.Method.invokeNative(Native Method) 02-11 12:15:14.162: E/AndroidRuntime(25911): at java.lang.reflect.Method.invoke(Method.java:511) 02-11 12:15:14.162: E/AndroidRuntime(25911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-11 12:15:14.162: E/AndroidRuntime(25911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-11 12:15:14.162: E/AndroidRuntime(25911): at dalvik.system.NativeStart.main(Native Method) 
+8
android android-fragments android-fragmentactivity
source share
3 answers

FragmentManager manages all Fragments at the Activity level, and their life cycle will be tied to this parent Activity . The Fragment child manager manages all the Fragments at the Fragment level, and their life cycle will be tied to this parent Fragment .

So, for the architecture of your phone, add InnerFragment to your Activity using getFragmentManager() . When an Activity destroyed forever (via the return / finish() button), the FragmentManager destroy and release the InnerFragment for you.

For tablet architecture, add InnerFragments to Fragment using getChildFragmentManager() (in the latest support library). When a Fragment destroyed forever, the FragmentManager destroy and free InnerFragments for you.

You do not need to manage the release and destruction of your Fragments yourself. I would recommend recording the life cycle events of your Activities and Fragments so that you can watch how they go through their states and ensure proper behavior.

+9
source share

The NullPointerException is thrown due to the fact that the activity handler is not configured from the FragmentManager, so the โ€œsolutionโ€ that will prevent the crash will be as follows:

 public void onDestroy(){ super.onDestroy(); try { Field mActivityField = getFragmentManager().getClass().getDeclaredField("mActivity"); mActivityField.setAccessible(true); mActivityField.set(getFragmentManager(), this); Field mPendingActionsField = getFragmentManager().getClass().getDeclaredField("mPendingActions"); mPendingActionsField.setAccessible(true); mPendingActionsField.set(getFragmentManager(), null); Field f = Activity.class.getDeclaredField("mHandler"); f.setAccessible(true); Handler handler = (Handler) f.get(this); handler.close(); } catch (Throwable e) { } } 
+1
source share

CASE: when you need to call a Fragment (child fragment) from another fragment (parent fragment)

always use getChildFragmentManager() instead of getFragmentManager inside the parent fragment.

Read documentation

0
source share

All Articles