0

I have a Main activity which I can not extends to a Fragment activity. In First I was showing my google map in it directly and that all was working fine. But then I have to used the fragments so I have converted my all other activities(Except MainActivity) into fragments and was successfully able to call them and replace them. But there comes a problem. When I needed to put my google map into a separate fragment and call it with my main activity start up.. it loads the map but it look likes it is not instantiating the map properly

My xml for MyMapFragment which contains map goes like this :

<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="com.abdulsalamali.letscoffee.MyMapFragment">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/map_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


        <fragment
            android:id="@+id/mapView"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/rl_address" />


    </RelativeLayout>

</FrameLayout>

and on activity created of this map I am trying to initializing map like this

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        createMapView();
        // and doing some stuff here such as camera animation 
}

private void createMapView() {
        /**
         * Catch the null pointer exception that
         * may be thrown when initialising the map
         */
        try {
            if (null == googleMap) {
                MapFragment mapFragment = new MapFragment();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
              //  transaction.add(R.id.rl_map_container, mapFragment).commit();
                googleMap= mapFragment.getMap();
                googleMap = ((SupportMapFragment) getChildFragmentManager()
                        .findFragmentById(R.id.mapView)).getMap();
                googleMap.setBuildingsEnabled(true);
                googleMap.setMyLocationEnabled(true);

                /**
                 * If the map is still null after attempted initialisation,
                 * show an error to the user
                 */
                if (null == googleMap) {
                    Toast.makeText(getActivity(),
                            "Error creating map", Toast.LENGTH_SHORT).show();

                }


            }
        } catch (NullPointerException exception) {
            Log.e("mapApp", exception.toString());
        }
    }

So it is not animating it just shows map , not setting current location , not enabling button for myLocationEnabled. Actually it returns googleMap instance as null.

I have read many things and got the hint of nested fragments but could not understand that How to make it work , I have searched many tutorials which they are using fragment activity which I do not want to use . So please help Me I think I am quite clear in my question.

stacy queen
  • 280
  • 1
  • 4
  • 19

1 Answers1

1

I am posting this as an Answer as I figured out it by myself but with different references which were not related to my case and were using different techniques: I have mistake in my xml only and that was i was using a fragment tag inside my xml of fragment which is supposed to be adding a map fragment in it.

so now I changed it in the following way.

<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="com.abdulsalamali.letscoffee.MyMapFragment">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/map_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


        <FrameLayout
            android:name="com.google.android.gms.maps.MapFragment"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/mapView"

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/rl_address" />


    </RelativeLayout>

</FrameLayout>

So in this way It started to work even on back button there is no app crash. I am posting it so that if any one falls in this condition have this post.

stacy queen
  • 280
  • 1
  • 4
  • 19