I get nullpointerException when trying to load google map on our app as below.also The java class extends from AppCompatActivty
and inside intialiseMap()
we try to getMap()
D/Profileactivity: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
And here is where we get the exception,inside placeMarker method
public void placeMarker(LatLongDetails user_latlongobj2,
final Context contextPlace) {
try {
if (googlemap == null) {
intialiseMap();
animatecamera(user_latlongobj);
}
if (LoginDetails.Address.length() < 1) {
LoginDetails.Address = "Getting your location .....";
}
//googlemap.clear();
marker = new MarkerOptions().position(
new LatLng(user_latlongobj2.user_latitude,
user_latlongobj2.user_longitude)).title(
LoginDetails.Address);
System.out.println("This is the Address" + LoginDetails.Address);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
if (googlemap != null) {
googlemap.addMarker(marker).showInfoWindow();
}else {
intialiseMap();
googlemap.addMarker(marker).showInfoWindow();
}
System.out.println("PLACING MARKER" + LoginDetails.Address);
if (marker == null || contextPlace == null) {
Intent in =new Intent(this,ProfileActivity1.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in);
}
else
fixmarker(marker, contextPlace);
} catch (Exception e) {
fixmarker(marker, contextPlace);
Log.d("Profileactivity", "" + e);
}
}
This is how we initialise Map
private void intialiseMap() {
try {
if (dialog == null)
dialog = ProgressDialog.show(ProfileActivity1.this, "",
getString(R.string.getting_location), true, true);
}catch(Exception e) {
Log.e("eybaba",""+e);
}
try {
if (googlemap == null) {
googlemap = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.mapfragment)).getMap();
googlemap.setInfoWindowAdapter(new CustomInfowindow(context));
// check if map is created successfully or not
if (googlemap == null) {
Toast.makeText(getApplicationContext(),
R.string.maps_create_error, Toast.LENGTH_SHORT)
.show();
}
}
} catch (Exception e) {
}
}