I've got 3 fragments, one NavigationDrawer, one MapFragment, and one user-defined "MapInfoFragment". I want the "MapInfoFragment" to appear semi-transparent over top of the MapFragment on certain events and disappear on others. I don't really care if I completely remove the fragment and create a new one each time, or I just change the visibility and information displayed. Currently I'm just trying to use FragmentManager's .hide() function, but I've also tried .remove and .detach with similar results.
Error:
03-18 14:28:10.965 24711-24711/com.[packageName].asTest D/AndroidRuntime﹕ Shutting down VM 03-18 14:28:10.965 24711-24711/com.[packageName].asTest E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.[packageName].asTest, PID: 24711 java.lang.NullPointerException: Attempt to write to field 'int android.app.Fragment.mNextAnim' on a null object reference at android.app.BackStackRecord.run(BackStackRecord.java:658) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Activity_main.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.[packageName].asTest.MainActivity"> <!-- As the main content view, the view below consumes the entire space available using match_parent in both dimensions. --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" map:cameraZoom="8" map:mapType="satellite" map:uiRotateGestures="false" android:layout_gravity="center_horizontal|top" /> </FrameLayout> <fragment android:id="@+id/navigation_drawer" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" android:name="com.[packageName].asTest.NavigationDrawerFragment" tools:layout="@layout/simple_list_item_1" /> <fragment android:id="@+id/map_info" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.[packageName].asTest.MapInfoFragment" /> </android.support.v4.widget.DrawerLayout>
Offending Code:
FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.hide(getFragmentManager().findFragmentById(R.id.map_info)); ft.commit();
I changed map_info_layout
to map_info
and there's no change in the error I'm getting. I'd previously had map_info_layout
defined as the id for the root FrameLayout of the MapInfoFragment's layout xml, so I think map_info_layout
and map_info
were pointing to the same thing.
Turned out that I had MapInfoFragment
extending android.support.v4.app.Fragment
instead of android.app.Fragment
and I was using the android.app.FragmentTransaction
instead of android.support.v4.app.FragmentTransaction
. Switched everything to android.app.
since I'm not interested in older device compatibility. Thanks Fllo.
Stop trying to hide null objects
To remove a fragment from the host, call remove() , passing in a fragment instance that was retrieved from the fragment manager through findFragmentById() or findFragmentByTag() . If the fragment's view was previously added to a container, the view is removed from the container at this point.
I know you've solved this yourself, but I want to explain how this error happens, since it just happened to me.
Basically you're calling hide()
, remove()
, etc., with a null value.
The error isn't obvious, and the stack trace doesn't originate from your own sources when this happens, so it's not trivial to realise what it is.
It also happens when you call FragmentManager.popBackstack()
without there being a fragment in the backstack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With