Issue
i am going to show user current location in map but i have following error in 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
my class extends following:
extends SupportMapFragment implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener
onCreateView:
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:
@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);
}
i really have no idea about this error, please help me.
Solution
Add this to your onCreateView():
super.onCreateView(inflater, container, savedInstanceState);
Your onCreateView() is useless without it. This is why it's likely that your mapView.onCreate(savedInstanceState);
is not called.
Make sure you initialize your mapView
object (new MapView
), it might be null.
Answered By - maspinu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.