mMap.setOnMapClickListener(this) 中的空对象错误;

Posted

技术标签:

【中文标题】mMap.setOnMapClickListener(this) 中的空对象错误;【英文标题】:Null object error in mMap.setOnMapClickListener(this); 【发布时间】:2016-06-17 08:23:22 【问题描述】:
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...
        


    


在这一行出现错误 mMap.setOnMapClickListener(this);

java.lang.RuntimeException:无法启动活动 组件信息com.example.mukesh.airpollution/com.example.mukesh.airpollution.MapsActivity: java.lang.NullPointerException:尝试调用虚拟方法'void com.google.android.gms.maps.GoogleMap.setOnMapClickListener(com.google.android.gms.maps.GoogleMap$OnMapClickListener)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在 android.app.ActivityThread.access$800(ActivityThread.java:151) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5254) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'无效 com.google.android.gms.maps.GoogleMap.setOnMapClickListener(com.google.android.gms.maps.GoogleMap$OnMapClickListener)' 在空对象引用上 在 com.example.mukesh.airpollution.MapsActivity.onCreate(MapsActivity.java:58) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在 android.app.ActivityThread.access$800(ActivityThread.java:151) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5254) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

【问题讨论】:

显然mMap 为空。 【参考方案1】:

首先将setOnMapClickListener移动到onMapReady(),,然后确保Map层在所有层中具有最高值,否则会调用最顶层的onClickListener。那应该这样做。让我知道是否有任何进一步的问题。 :)

【讨论】:

【参考方案2】:

只需删除 onMapClick 并在 onMapReady() 中实现 setonMapClickListner。现在它应该可以正常工作了。

【讨论】:

以上是关于mMap.setOnMapClickListener(this) 中的空对象错误;的主要内容,如果未能解决你的问题,请参考以下文章