3

I am using FrameLayout to show fragments

 <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/bottom_layout"
        android:background="@android:color/white" >
    </FrameLayout>

and to open fragment i am using this

ft.add(R.id.fragment_content, frag);
ft.commit();
fm.beginTransaction();

One fragment contains map so i am using these lines in layout file

<fragment
        android:id="@+id/trafic_Alarm_main_mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.Utilities.MySupportMapFragment"
        android:layout_alignParentTop="true" />

and this is my Java code

try{
mMap=(MySupportMapFragment)TabsActivity.fm.findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap();
}catch (Exception e){
mMap=((MySupportMapFragment)getChildFragmentManager().findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap();
}

Now, if getChildFragmentManager() gets called and i switch to another fragment that uses getSupportFragmentManager to begin transaction causes crash . Provding logs

01-16 18:36:15.690    7294-7294/com.TrafikAlarm E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.TrafikAlarm, PID: 7294
    java.lang.IllegalStateException: Recursive entry to executePendingTransactions
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I am using android studio and compiling these libraries ,

compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.android.support:appcompat-v7:21.0.3'

When i was using eclipse i did not need to use getChildFragmentManager and everything was working fine .

Thanks in advance.

Jan
  • 859
  • 7
  • 30

5 Answers5

1
  1. You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.
  2. FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher).
Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
  • I tried using this , i am getting same error mMap=((MySupportMapFragment)getParentFragment().getFragmentManager().findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap(); java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.Fragment.getFragmentManager()' on a null object reference – Jan Jan 27 '15 at 10:37
1

Since your context is a fragment, have you tried calling super on your child fragment manager before you use it?

super(fragment.getChildFragmentManager());

Marcin D
  • 918
  • 7
  • 14
  • Where should i call it ? – Jan Jan 29 '15 at 12:20
  • first line in the method where this resides: `try{ mMap=(MySupportMapFragment)TabsActivity.fm.findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap(); }catch (Exception e){ mMap=((MySupportMapFragment)getChildFragmentManager().findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap(); }` – Marcin D Jan 29 '15 at 14:52
  • I am not using it in any overrided method. The parent overrided method is onCreateView and that does not support super(fragment.getChildFragmentManager()); – Jan Jan 30 '15 at 11:49
1

Hi it appears that you are having a recursive problem Recursive entry to executePendingTransactions since you are reapeting the call more than once, and in an Exception Handling which by practice is wrong to catch an Exeption error and try to "do something()"

try{

mMap=(MySupportMapFragment)TabsActivity.fm.findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap();

}catch (Exception e){

mMap=((MySupportMapFragment)getChildFragmentManager().findFragmentById(R.id.trafic_Alarm_main_mapView)).getMap();

}

Remove the unnecessary call on the catch area, If a new error appear make sure to read the log cat, and do the adding/removing of fragment in the correct @override such as @onCreate of your activity working with the fragment.

Hope it helps.

Joel
  • 838
  • 5
  • 12
0

Layout fragments are great for one time, add and forget kind of usage. Beyond that You must manage fragments using java code.

Simply stick to some rules when adding, replacing fragments:

Int frameId = // id of host view
Fragment manager = // fragment manager, of Activity or a Fragment
String tag = // unique tag for the fragment
String clazz = // class name of the fragment
Bundle args = // initial data to be supplied to fragment instance

Fragment frg = manager.findFragmentbyTag(tag);

if(frg == null){
  frg = Fragment.instantiate(context,clazz,args);
}

if(!frg.isAdded()){
   manager.beginTransaction().add(frameId, frg, tag).commit();
}

If a Fragment has also to show a nested fragment on start, You can use above code in its onViewCreated().

FragmentManager is asynchronous in nature, after commit(), it doesn't immediately apply fragment changes. The commit is scheduled to happen sometime in future. executePendingTransactions() is available, but should be used with extreme care. It makes all scheduled transactions run immediately, and when they are running, you must not call this again.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0

I have found the bug . I was using FragmentActivity manager to return map object to remove map fragment that was causing problem and logs were not . So , i changed

TabsActivity.fm.beginTransaction().remove((MySupportMapFragment) TabsActivity.fm.findFragmentById(R.id.trafic_Alarm_main_mapView)).commit();

to

TabsActivity.fm.beginTransaction().remove((MySupportMapFragment) getChildFragmentManager().findFragmentById(R.id.trafic_Alarm_main_mapView)).commit();
Jan
  • 859
  • 7
  • 30