【发布时间】:2020-06-26 08:24:36
【问题描述】:
我收到此错误
NullPointerException:尝试在空对象引用上调用虚拟方法 'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)
并且应用程序由于此错误而崩溃。我想提一下,我在互联网上搜索并在这里寻找答案,但没有找到任何东西
这是我的代码:
public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;
private GoogleMap googleMap;
private ArrayList<LatLng> arrayPoints = null;
PolylineOptions polylineOptions;
private boolean checkClick = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
arrayPoints = new ArrayList<LatLng>();
// 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);
// display zoom map
googleMap.setOnMapClickListener(this);
googleMap.setOnMapLongClickListener(this);
googleMap.setOnMarkerClickListener(this);
}
public GoogleMap getmMap() {
return mMap;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
googleMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(final LatLng latLng) {
if (checkClick == false) {
LatLng currentLocation = null;
Marker marker = googleMap.addMarker(new MarkerOptions().position(currentLocation)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.point)));
}
}
});
}
@Override
public void onMapLongClick(LatLng latLng) {
googleMap.clear();
arrayPoints.clear();
checkClick = false;
}
@Override
public boolean onMarkerClick(Marker marker) {
// TODO Auto-generated method stub
System.out.println("Marker lat long=" + marker.getPosition());
System.out.println("First postion check" + arrayPoints.get(0));
System.out
.println("**********All arrayPoints***********" + arrayPoints);
if (arrayPoints.get(0).equals(marker.getPosition())) {
System.out.println("********First Point choose************");
countPolygonPoints();
}
return false;
}
private void countPolygonPoints() {
if (arrayPoints.size() >= 3) {
checkClick = true;
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(arrayPoints);
polygonOptions.strokeColor(Color.BLUE);
polygonOptions.strokeWidth(7);
polygonOptions.fillColor(Color.CYAN);
Polygon polygon = googleMap.addPolygon(polygonOptions);
}
}
@Override
public void onMapClick(LatLng latLng) {
}
}
【问题讨论】:
标签: java android android-studio google-maps nullpointerexception