Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 试图在谷歌地图上的我的当前位置上设置标记会导致Nullpointer异常_Android_Google Maps_Nullpointerexception_Locationmanager - Fatal编程技术网

Android 试图在谷歌地图上的我的当前位置上设置标记会导致Nullpointer异常

Android 试图在谷歌地图上的我的当前位置上设置标记会导致Nullpointer异常,android,google-maps,nullpointerexception,locationmanager,Android,Google Maps,Nullpointerexception,Locationmanager,我已经在我的Android项目中集成了谷歌地图。我正在设备上查看地图。我想将标记设置为我的当前位置。我已经完成了以下编码,但它在第43行(即下一行)上给了我一个空指针异常 mMap.addMarker(新标记选项() .position(新车床(location.getLatitude(),location.getLongitude()) .title(“Hello world”) 我的代码如下。请一步一步地告诉我出了什么问题 public class location extends

我已经在我的Android项目中集成了
谷歌地图
。我正在设备上查看地图。我想将标记设置为我的当前位置。我已经完成了以下编码,但它在第43行(即下一行)上给了我一个
空指针异常

mMap.addMarker(新标记选项()
.position(新车床(location.getLatitude(),location.getLongitude())
.title(“Hello world”)

我的代码如下。请一步一步地告诉我出了什么问题

     public class location extends Activity implements LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
private static final long MIN_TIME = 400;
private static final float MIN_DISTANCE = 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_location);

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.the_map)).getMap();
    mMap.setMyLocationEnabled(true);

    //mMap.addMarker(new MarkerOptions());

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); 
    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    mMap.addMarker(new MarkerOptions()
    .position(new LatLng(location.getLatitude(), location.getLongitude()))
    .title("Hello world"));


}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
    mMap.animateCamera(cameraUpdate);

   // locationManager.removeUpdates(this);


}

你的代码似乎是正确的。只需检查您的位置对象,它可能为空。

请尝试下面的代码,它对我有用

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_location);
    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    }else { // Google Play Services are available

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        LocationListener locationListener = new LocationListener() {
          void onLocationChanged(Location location) {
          // redraw the marker when get location update.
          drawMarker(location);
        }

        if(location!=null){
           //PLACE THE INITIAL MARKER              
           drawMarker(location);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
    }
}

private void drawMarker(Location location){
    googleMap.clear();
    LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());
    googleMap.addMarker(new MarkerOptions()
    .position(currentPosition)
    .snippet("Lat:" + location.getLatitude() + "Lng:"+ location.getLongitude()));
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
    .title("ME"));
}
@Override
public void onLocationChanged(Location location) {
    // Add a marker in Sydney and move the camera
    mLocation = location;
    LatLng myLocation = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
    mMap.addMarker(new MarkerOptions()
            .position(myLocation)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
            .title("My Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
    Log.d("location", "Latitude:" + mLocation.getLatitude() + "\n" + "Longitude:" + mLocation.getLongitude());
}