我将在地图中显示用户当前位置,但在onResume()(super.onResume())中有以下错误:
@Override
public void onResume() {
**super.onResume();**
mapView.onResume();
mGoogleApiClient.connect();
}
java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.d.fk.o()' on a null object reference我的课程扩展如下:
extends SupportMapFragment implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListeneronCreateView:
mapView=(MapView)view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds覆盖:
@Override
public void onConnected(Bundle bundle) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
else {
handleNewLocation(location);
};
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(mActivity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i("TAG", "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
private void handleNewLocation(Location location) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
map.addMarker(options);
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}我真的不知道这个错误,请帮帮我。
发布于 2015-11-07 18:57:38
将此添加到onCreateView()中
super.onCreateView(inflater, container, savedInstanceState);
没有它,你的onCreateView()是无用的。这就是为什么不调用您的mapView.onCreate(savedInstanceState);的原因。
请确保初始化mapView对象(new MapView),它可能为null。
https://stackoverflow.com/questions/33586137
复制相似问题