Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.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 谷歌地图-空指针异常_Android_Nullpointerexception_Gps_Google Maps Markers - Fatal编程技术网

Android 谷歌地图-空指针异常

Android 谷歌地图-空指针异常,android,nullpointerexception,gps,google-maps-markers,Android,Nullpointerexception,Gps,Google Maps Markers,我已经尝试过前面提到的解决方案 public class LocationActivity extends FragmentActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback {

我已经尝试过前面提到的解决方案

 public class LocationActivity extends FragmentActivity implements
                LocationListener,
                GoogleApiClient.ConnectionCallbacks,
                GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback {

        private static final String TAG = "LocationActivity";
        private static final long INTERVAL = 1000 * 60 * 1; //1 minute
        private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
        Button btnFusedLocation;
        TextView tvLocation;
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        Location mCurrentLocation;
        String mLastUpdateTime;
        GoogleMap googleMap;

        protected void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (!isGooglePlayServicesAvailable()) {
                finish();
            }
            createLocationRequest();
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            setContentView(R.layout.activity_location_google_map);
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            fm.getMapAsync(this);
        }

        @Override
        public void onMapReady(GoogleMap map) {

            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            map.setMyLocationEnabled(true);
            map.setTrafficEnabled(true);
            map.setIndoorEnabled(true);
            map.setBuildingsEnabled(true);
            map.getUiSettings().setZoomControlsEnabled(true);
        }


        private void addMarker() {
            MarkerOptions options = new MarkerOptions();
            IconGenerator iconFactory = new IconGenerator(this);
            iconFactory.setStyle(IconGenerator.STYLE_PURPLE);
            options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime)));
            options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());

            LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
            options.position(currentLatLng);
            Marker mapMarker = googleMap.addMarker(options);
            long atTime = mCurrentLocation.getTime();
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
            mapMarker.setTitle(mLastUpdateTime);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
                    13));
        }

        }
    }

我正在尝试添加一个带有标签的标记,作为使用gps测量纬度和经度的时间。但是,当调用这个方法时,我得到一个错误:addMarker(..)是在null对象上调用的。在调试模式下运行时,纬度和经度显示正确。很明显,映射本身是空的。有人能告诉我如何处理这个问题吗?我已经给出了相关的代码。提前感谢:)

初始化googleMap内部已于4月1日

谷歌地图=地图


检查这里:从代码中不清楚何时何地调用addMarker函数。是在调用onMapReady之后吗?@Override public void onLocationChanged(Location-Location){mCurrentLocation=Location;mLastUpdateTime=DateFormat.getTimeInstance().format(new-Date());addMarker();}显然
googleMap
null
@Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);
        map.setTrafficEnabled(true);
        map.setIndoorEnabled(true);
        map.setBuildingsEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(true);
    }