0

Hi I am trying to create a app where I am will be displaying map in a fragment using SupportMapFragment but I am getting NullPointerException. Here is the full error code

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at obs.sharemytable.fragment.Search.onCreateView(Search.java:49)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6918)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Search.java:

public class Search extends Fragment{
  GoogleMap mGoogleMap;

  public static Search newInstance() {
        Search fragment = new Search();
        return fragment;
    }

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

        final View view = inflater.inflate(R.layout.search_layout, null, false);


        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map_container);
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mGoogleMap = googleMap;

                // Add a marker in Sydney and move the camera
                LatLng sydney = new LatLng(-34, 151);
                mGoogleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
                mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            }
        });

        return view;
    }
}

search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include
        android:id="@+id/searchToolbar"
        layout="@layout/toolbar" />
    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.google.android.gms.maps.MapView>

</LinearLayout>

Can someone figure what mistake I have done here.

USER9561
  • 1,084
  • 3
  • 15
  • 41
Jeffrey Rajan
  • 4,391
  • 4
  • 27
  • 37

4 Answers4

0

Try getting the FragmentManager from the activity and using it to get your map:

FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();

final SupportMapFragment mapFragment  = (SupportMapFragment) mFragmentManager
            .findFragmentById(R.id.map_container);
Miguel Benitez
  • 2,322
  • 10
  • 22
0
SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().fragmentManager.findFragmentById(R.id.location_map))
Michaël
  • 3,679
  • 7
  • 39
  • 64
0

Thanks @MojioMS now its working for me. Here is the changes I made to work.

Change this search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include
        android:id="@+id/searchToolbar"
        layout="@layout/toolbar" />
    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.google.android.gms.maps.MapView>

</LinearLayout>

to

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include
        android:id="@+id/searchToolbar"
        layout="@layout/toolbar" />
    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment">
    </fragment>

</LinearLayout>
Jeffrey Rajan
  • 4,391
  • 4
  • 27
  • 37
0

When you use delay after initializing SupportMapFragment then there will not null pointer exception.

Muhammad Noman
  • 1,842
  • 19
  • 14