-1

i have a class which is extended from a fragment ... and i want to getSupportFragmentManager() for finding the map fragment... this is the fragment xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="myproject.BlankFragment">

<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
    android:text="this is ths map " />

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:id="@+id/mapFrag"
    tools:context="pk.edu.riu.mrcab.mrcabv1.MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    />

this is the fragment class

public class BlankFragment extends Fragment {
private GoogleMap mMap; 

public BlankFragment() {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

    return rootView;
}
private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        if (mMap != null) {
        }
    }
}
Muhammad Jamal
  • 442
  • 4
  • 21

3 Answers3

1

Whenever you are trying to access a nested Fragment, you should call getChildFragmentManager() instead of getSupportFragmentManager(). Please note that you should use the support library Fragment, unless you're supporting API 17+.

fractalwrench
  • 4,028
  • 7
  • 33
  • 49
  • can u please tell me when i use "map.addCircle() " function is gives the error in console.. not at the time of compilation ... but when i use the frgment in emulator or phone.. console error: " at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addCircle(Unknown Source) at com.google.android.gms.maps.GoogleMap.addCircle(Unknown Source)" – Muhammad Jamal Dec 11 '15 at 18:20
0

Did you try

FragmentManager fm = getActivity().getSupportFragmentManager();
// and then depending on your case
fm.findFragmentById(R.id.yourId);
// or
fm.findFragmentByTag("YOUR_TAG");
// or
fm.getFragments(); // and then search the right one

This is the way I'm usually doing it.

Desdroid
  • 300
  • 2
  • 9
0

use this

private FragmentActivity myContext;

override onAttach method of your fragment :

@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}

and call this method when you need

FragmentManager fragManager = myContext.getSupportFragmentManager(); //if lib you are using is support v4

or use

FragmentManager fragManager = myContext.getFragmentManager();

or else you can directly call this method.

getFragmentManager() 
Jayanth
  • 5,954
  • 3
  • 21
  • 38