0

I have a layout that actually shows the map on the app

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/fragment_create_instant_album_create_button_text_view" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical"
            android:paddingLeft="30dp"
            android:paddingRight="30dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="@string/instant_album_info"
                android:textColor="@color/pico_clay" />

            <EditText
                android:id="@+id/fragment_create_instant_album_album_name_text_view"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/blue_border_edittext_unselected"
                android:hint="@string/album_name"
                android:paddingLeft="15dp"
                android:textColor="@color/pico_dark_grey"
                android:textColorHint="@color/pico_grey_blue"
                android:textSize="@dimen/text_medium" />

            <fragment
                android:id="@+id/fragment_create_instant_album_map_fragment"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_marginTop="15dp"
                class="com.google.android.gms.maps.SupportMapFragment"
                android:background="@drawable/blue_border_edittext_unselected"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:tag="map" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:gravity="center_vertical"
                android:paddingTop="8dp" >

                <RadioGroup
                    android:id="@+id/fragment_create_instant_album_privacy_radio_group"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                </RadioGroup>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

    <com.coapps.pico.fonts.RobotoTextView
        android:id="@+id/fragment_create_instant_album_create_button_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/pico_salmon"
        android:gravity="center"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:text="@string/create_instant_album"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textColor="@android:color/white" />

</RelativeLayout>

but when using this code to get the map fragment, it always return null (although I'm seeing the map)

albumName = (TextView) view
        .findViewById(R.id.fragment_create_instant_album_album_name_text_view);
createAlbum = (TextView) view
        .findViewById(R.id.fragment_create_instant_album_create_button_text_view);
mapFragment = (SupportMapFragment) mainActivity.getSupportFragmentManager().findFragmentByTag("map");

Why? What am I doing wrong?

Steve Trout
  • 9,261
  • 2
  • 19
  • 30
Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154

2 Answers2

1

Try this to get your map

GoogleMap mMap = ((SupportMapFragment) getActivity()
                .getSupportFragmentManager().findFragmentById(R.id.fragment_create_instant_album_map_fragment))
                .getMap();

If this still gives you null, then try this alternate.First of all, replace your map fragment in xml with this FrameLayout

 <FrameLayout
    android:id="@+id/fl_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
     />

and then in your onActivityCreated of your Fragment, create your SupportMapFragment like this

     SupportMapFragment fragment= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.ll_map);

    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.fl_map, fragment).commit();
    }

    GoogleMap mMap=fragment.getMap();

and also in onDetach() of your Fragment, put this code

@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = Fragment.class
                .getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
  • getActivity() .getSupportFragmentManager().findFragmentById always return null – Asaf Nevo Nov 07 '14 at 05:47
  • strange its working for me, have you tried debugging but actually is coming out to be null and also you are doing that all in onCreateView of a Fragment? – Mukesh Rana Nov 07 '14 at 05:59
  • Although it should have worked for you, but i'll provide you another solution if this fails. But be sure first that this is not working by debugging your code and find out what actually the reason is – Mukesh Rana Nov 07 '14 at 06:10
  • okay, then wait i'll provide you another solution,hope that will work – Mukesh Rana Nov 07 '14 at 06:14
0

Did you add meta data in your Manifest file and Internet permission

  <uses-permission android:name="android.permission.INTERNET" />

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="******2wuOppWIMVpr8DF6u6ydKf********" />

You can check this link http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

Dhruba Bose
  • 446
  • 2
  • 7
  • Please read question again is not related map not shown which clearly say getting null when try to find map fragment using findFragmentByTag(). – Haresh Chhelana Nov 07 '14 at 05:39