在GoogleMap.setMyLocationEnabled(布尔值)上获取NullPointerException



我得到这个错误

NullPointerException:尝试在空对象引用上调用虚拟方法"void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(布尔值(">

并且该应用程序由于这个错误而崩溃。我想提到的是,我在互联网上搜索了答案,但没有找到任何

这是我的代码:

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) {
}
}

您的代码中存在多个问题。

首先,您使用了两个GoogleMap变量,但都没有正确初始化。你只需要一个。

private GoogleMap mMap;

其次,为了使用setMyLocationEnabled,您需要在运行时向用户请求位置权限:

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}

然后将currentLocation设置为null,并将此空变量传递给新标记。相反,使用实际的latLng:

mMap.addMarker(new MarkerOptions().position(latLng));

最后,您实际上并没有将任何项目添加到arrayPoints阵列列表中。我想你想做这样的事情:

arrayPoints.add(marker.getPosition());

下面是完整的工作代码。

import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import java.util.ArrayList;
public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;
private ArrayList<LatLng> arrayPoints = null;
private boolean checkClick = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
arrayPoints = new ArrayList<>();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(final LatLng latLng) {
if (!checkClick) {
mMap.addMarker(new MarkerOptions().position(latLng));
}
}
});
}
@Override
public void onMapLongClick(LatLng latLng) {
mMap.clear();
arrayPoints.clear();
checkClick = false;
}
@Override
public boolean onMarkerClick(Marker marker) {
arrayPoints.add(marker.getPosition());
System.out.println("Marker lat long=" + marker.getPosition());
System.out.println("First position 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);
mMap.addPolygon(polygonOptions);
}
}
@Override
public void onMapClick(LatLng latLng) {
}
}

希望这能有所帮助!

相关内容

  • 没有找到相关文章

最新更新