Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findFragmentById for SupportMapFragment returns null in Android Studio

Recently I migrated a project from Eclipse to Android Studio. Everything is setup and working fine except for my one fragment which uses a SupportMapFragment. The below findFragmentById (which worked when building in Eclipse) is now returning null :(

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SupportMapFragment m = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.safety_map));

snippet of xml...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:map="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >

     <fragment
         android:id="@+id/safety_map"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_marginBottom="40dp"
         map:cameraTargetLat="@string/livesafe_latitude"
         map:cameraTargetLng="@string/livesafe_longitude"
         map:uiZoomControls="false"
         class="com.google.android.gms.maps.SupportMapFragment"/>

Here are my dependencies in my build.gradle:

 dependencies {
    //google analytics
    compile 'com.google.apis:google-api-services-analytics:v3-rev103-1.19.0'
    //support library for api 10
    compile 'com.android.support:support-v4:21.0.0'
    //google play services
    compile 'com.google.android.gms:play-services:6.1.11'
    compile project(':facebook')
    compile files('libs/android-support-multidex.jar')
    compile files('libs/aws-android-sdk-1.6.0-debug.jar')
    compile files('libs/FlurryAnalytics_3.3.2.jar')
 }

I haven't changed any code in the xml file or the Fragment class that previously worked in Eclipse.

like image 962
J_Sizzle Avatar asked Oct 27 '14 21:10

J_Sizzle


3 Answers

Judging by the fact that you're overriding Fragment.onActivityCreated(), I take it that your layout containing the map fragment is the layout for your Fragment. In that case, the SupportMapFragment is a child fragment of your hosting Fragment. When you attempt to retrieve it, you're using the Activity FragmentManager. You should instead use your Fragment's FragmentManager:

For example, this:

SupportMapFragment m = ((SupportMapFragment) getActivity()
        .getSupportFragmentManager().findFragmentById(R.id.safety_map));

becomes:

SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager()
        .findFragmentById(R.id.safety_map));
like image 126
Kevin Coppock Avatar answered Oct 16 '22 18:10

Kevin Coppock


Yes. It's not work for targetSdkVersion 21, if your Map fragment is inner part of Fragment (due to some issues, that mentioned this and this).

As temporary resolving can advice such trick:

public class MyFragment extends Fragment {

    private SupportMapFragment fragment;
    private GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_with_map, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
        if (fragment == null) {
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_container, fragment).commit();
        }
    }
}
like image 38
cosic Avatar answered Oct 16 '22 18:10

cosic


The following code worked for me. I was using Google Map in a Fragment:

 SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager() .findFragmentById(R.id.safety_map));
like image 4
T. pac Avatar answered Oct 16 '22 18:10

T. pac