How to include Googlemap fragment in a Fragment class?

192 Views Asked by At

I want to add markers and draw a polyline between two locations in the map, The issue is an exception is thrown when I try to refer the Googlemap from the Fragment class..

It's like this, I have a class which extends the Fragment class and the view of fragment class is set using a normal xml layout. Moreover, this layout contains a fragment and this is where the google map is located.

Here is the code of the layout of the Fragment which holds the map-fragment;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="20dp"
    >

    <EditText
        android:id="@+id/from"
        android:layout_width="300dp"
        android:layout_gravity="center"
        android:hint="Enter the starting location"
        android:layout_height="wrap_content"></EditText>

    <Button
        android:id="@+id/btn_showDirections"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:text="Show Directions"
        android:layout_height="wrap_content" />

    <fragment
        android:layout_width="match_parent"
        android:tag="HelloMap"
        android:layout_height="300dp"
        android:layout_margin="10dp"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        />





</LinearLayout>

This is the class which is connected to the given layout file

 public class frg_planVisit_directionsToZoo extends Fragment {

        GoogleMap mMap;

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


            try{
                mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
            }
            catch (Exception exp)
            {
                Log.i("Chin",exp.toString());
            }

            return inflater.inflate(R.layout.frg_planvisit_directions_to_zoo,container,false);
        }

}

The fragment which holds the map fragment, is connected to an activity and it works fine. The exception is thrown at the code where I get a reference to the google map.

mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

If this is wrong can someone please help me to get a reference to the map ? I'm new to Android.

I will post the some important stuff in the stack trace for your convenience.

*12-17 04:00:24.533  19369-19369/com.android.ZooCeylon I/Chin﹕ java.lang.NullPointerException
12-17 04:00:25.423  19369-19374/com.android.ZooCeylon E/dalvikvm﹕ GC_CONCURRENT freed 359K, 8% free 13026K/14087K, paused 13ms+4ms, total 40ms
12-17 04:00:25.583  19369-19374/com.android.ZooCeylon E/dalvikvm﹕ GC_CONCURRENT freed 870K, 11% free 13647K/15239K, paused 7ms+15ms, total 64ms
12-17 04:00:26.013  19369-19369/com.android.ZooCeylon W/System.err﹕ Invalid int: ""
12-17 04:00:27.943  19369-19369/com.android.ZooCeylon I/Choreographer﹕ Skipped 207 frames!  The application may be doing too much work on its main thread.
12-17 04:00:30.173  19369-19614/com.android.ZooCeylon W/System.err﹕ java.lang.NullPointerException*
2

There are 2 best solutions below

0
On

As far as I remember, map is initialised after some time on back thread. There are some callbacks for it. You could use googles examples or create map activity from your ide to check how it should be done.

0
On

As Eugen Pechanec said first of all need to move the code in OnActivityCreate() instead OncreateView(),

Also you must ensure that you have google play services available on your phone otherwise you will get the GoogleMap null. You can add below code to check the playservices installed or not.

if(map == null){
    int isEnabled = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
    if (isEnabled != ConnectionResult.SUCCESS) {
           GooglePlayServicesUtil.getErrorDialog(isEnabled, getActivity(), 0);
        }
       }
    else{
        //Continue what you were doing.
      }