Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null object error in mMap.setOnMapClickListener(this);

package com.example.mukesh.airpollution;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.maps.android.PolyUtil;
import java.util.List;

import java.util.ArrayList;

public class MapsActivity extends FragmentActivity
        implements OnMapClickListener, OnMapReadyCallback {

    //final int RQS_GooglePlayServices = 1;
    private GoogleMap mMap;
    //private GoogleMap googleMap;


    boolean markerClicked;
    PolygonOptions polygonOptions;
    Polygon polygon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is      ready to be used.
        //  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
        //        .findFragmentById(R.id.map);
        // mapFragment.getMapAsync(this);


        
        MapFragment mapFragment = (MapFragment)        getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync((OnMapReadyCallback) this);

        mMap.setOnMapClickListener(this);
      //  MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
       // mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        ArrayList<Polygon> polygonList = new ArrayList<>();
        Polygon polygon = mMap.addPolygon(new PolygonOptions()
                        .add(new LatLng(28.555335, 76.798553), new LatLng(28.830117, 76.935883), new LatLng(28.882919, 77.080078), new LatLng(28.868489, 77.220154), new LatLng(28.714438, 77.328644), new LatLng(28.519141, 77.3698434), (new LatLng(28.399615, 77.196808)), (new LatLng(28.555335, 76.798553)))
                        .strokeColor(Color.RED)

                //    .fillColor(Color.BLUE)
                //
        );



        // ArrayList<Polygon> polygonList = new ArrayList<>();
        Polygon p1 = mMap.addPolygon(new PolygonOptions()
                .add(new LatLng(28.882919, 77.080078), new LatLng(28.752972,  77.078705), new LatLng(28.714438, 77.328644), new LatLng(28.868489, 77.220154),new LatLng(28.882919, 77.080078))

                .strokeColor(Color.RED)

                .fillColor(Color.BLUE));

        // ArrayList<Polygon> polygonList = new ArrayList<>();
        Polygon p2 = mMap.addPolygon(new PolygonOptions()
                        .add(new LatLng(28.882919, 77.080078), new LatLng(28.752972, 77.078705), new LatLng(28.830117, 76.935883), new LatLng(28.882919, 77.080078))

                        .strokeColor(Color.RED)

                //.fillColor(Color.BLUE)
        );
        Polygon p3 = mMap.addPolygon(new PolygonOptions()
                        .add(new LatLng(28.555335, 76.798553), new LatLng(28.752972, 77.078705), new LatLng(28.830117, 76.935883), new LatLng(28.555335, 76.798553))

                        .strokeColor(Color.RED)

                // .fillColor(Color.BLUE)
        );
        final LatLng SYDNEY = new LatLng(28.6139,77.2090);
        final LatLng MOUNTAIN_VIEW = new LatLng(28.6387, 77.0941);

        final LatLngBounds.Builder bounds;
        final LatLng s=new LatLng(28.643353 ,77.446747);
        bounds = new LatLngBounds.Builder();
        bounds.include(new LatLng(28.643353, 77.446747));
        bounds.include(new LatLng(28.509488, 76.823273));



        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 10));


        mMap.animateCamera(CameraUpdateFactory.zoomIn());


        mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);


        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(MOUNTAIN_VIEW)      
                .zoom(10)                  
                .bearing(90)                
                .tilt(30)                  
                .build();                  
                                                                                           mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));







        mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {

                                           @Override
                                           public void onCameraChange(CameraPosition arg0) {
                                               // Move camera.
                                               // googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(),
                                               if (arg0.zoom >= 10) ;

                                               else if (arg0.zoom < 9) {
                                                   CameraPosition cameraPosition1 = new CameraPosition.Builder()
                                                           .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
                                                           .zoom(12)                   // Sets the zoom
                                                           .bearing(90)                // Sets the orientation of the camera to east
                                                           .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                                                           .build();                   // Creates a CameraPosition from the builder
                                                   mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition1));

                                               }
                                               // Remove listener to prevent position reset on camera move.
                                               //  googleMap.setOnCameraChangeListener(googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(),12)));


                                               //googleMap.(latlngbounds);
                                           }

                                       }

        );

    }



    @Override
    public void onMapClick(LatLng point) {
        Polygon polygon = mMap.addPolygon(new PolygonOptions()
                .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0)));

        Polygon p1 = mMap.addPolygon(new PolygonOptions()
                .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0)));

        Polygon p2 = mMap.addPolygon(new PolygonOptions()
                .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0)));

        Polygon p3 = mMap.addPolygon(new PolygonOptions()
                .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0)));


        //CHANGE POLYGON SPECIFICATIONS
        //ADD ALL POLYGONS


        List<LatLng> points = polygon.getPoints();
        List<LatLng> points1 = p1.getPoints();
        List<LatLng> points2 = p2.getPoints();
        List<LatLng> points3 = p3.getPoints();


        if (PolyUtil.containsLocation(point, points, false)) {
            //Do SOMETHING...
        } else if (PolyUtil.containsLocation(point, points1, false)) {
            //Do SOMETHING...
        }
        if (PolyUtil.containsLocation(point, points2, false)) {
            //Do SOMETHING...
        }
        if (PolyUtil.containsLocation(point, points3, false)) {
            //Do SOMETHING...
        }


    }

}

Getting error in this line
mMap.setOnMapClickListener(this);

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mukesh.airpollution/com.example.mukesh.airpollution.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnMapClickListener(com.google.android.gms.maps.GoogleMap$OnMapClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnMapClickListener(com.google.android.gms.maps.GoogleMap$OnMapClickListener)' on a null object reference at com.example.mukesh.airpollution.MapsActivity.onCreate(MapsActivity.java:58) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  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:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

like image 272
sujeet14108 Avatar asked Feb 07 '23 14:02

sujeet14108


1 Answers

First of all move the setOnMapClickListener to onMapReady(), then ensure that the Map layer has the highest value among all layers, as otherwise the onClickListener of the topmost layer will be called. That should do it. Let me know if there is any further issue. :)

like image 175
Siddharth Sharma Avatar answered Feb 10 '23 02:02

Siddharth Sharma