0

I am getting an error

Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

but i have done everything as per site i dont know whats the error please help I have done things in OnViewCreated and used child fragment also still the error is coming

fragment contact us

 package com.example.tanis.myapplication;


    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;


    /**
     * A simple {@link Fragment} subclass.
     */
    public class ContactUs extends Fragment implements OnMapReadyCallback {

        GoogleMap map;
        public ContactUs() {
            // Required empty public constructor
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate( R.layout.fragment_contact_us, container, false );



            return v ;
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated( view, savedInstanceState );
            SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById( R.id.map1 );
            mapFragment.getMapAsync( this );

        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            map = googleMap;
            LatLng pp = new LatLng(  28.6403159,77.376114);
            MarkerOptions options = new MarkerOptions();
            options.position( pp ).title( "WebTechniq" );
            map.addMarker( options );
            map.moveCamera( CameraUpdateFactory.newLatLng( pp ) );

        }
    }

XML

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tanis.myapplication.ContactUs">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout 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"
        android:id="@+id/map1"
        tools:context=".MapsActivity"
        class="com.google.android.gms.maps.SupportMapFragment" />

</android.support.constraint.ConstraintLayout>
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29

2 Answers2

0

Change your RelativeLayout to fragment.

To use SupportMapFragment you always need to have a fragment in your xml where it can put it's layout.

For more information about it refere to the official document of the android

Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24
0

In fragment the SupportMapFragment not working... to solved this problem Use MapView.. for use MapView in fragment see this answer... https://stackoverflow.com/a/19354359/9868485

code4rox
  • 941
  • 9
  • 34