Instantiating the Geocoder causes my app to crash with NullPointerException.
This is my code (only single Activity):
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private final int MAX_RESULTS = 1;
//if I comment out the following line, app will not crash
private Geocoder geocoder = new Geocoder(getApplicationContext()); //line 27
private GoogleMap gmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
setUpMap(map);
gmap = map;
}
...
}
I have tried new Geocoder(getBaseContext()), new Geocoder(this) and new Geocoder(MapsActivity.this) but they all crash. How to fix this?
logcat:
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
at android.location.Geocoder.<init>(Geocoder.java:83)
at android.location.Geocoder.<init>(Geocoder.java:95)
at rubiks.cres.MapsActivity.<init>(MapsActivity.java:27)
Move the initialization in onCreate :
private Geocoder geocoder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
geocoder = new Geocoder(this);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
getApplicationContext() is likely to return null if the Activity creation is not finished.
Related
I followed a tutorial but the codes did not work well. On the tutorial, he only types the BottomSheetDialog(); and doesn't have requirements inside the parenthesis but on my activity, it requires to put something there and I don't know it.
private GoogleMap mMap;
ArrayList<LatLng> MarkerPoints;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
double mLatitude;
double mLongitude;
private FirebaseAuth mAuth;
private Button btnShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Initialize Firebase Auth
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Initializing
MarkerPoints = new ArrayList<>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
btnShow = (Button) findViewById(R.id.bottomsheet);
btnShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog();
bottomSheetDialog.show(getSupportFragmentManager(),bottomSheetDialog.getTag());
}
});
}
According to the BottomSheetDialog code, only one these constructors are supported:
public BottomSheetDialog(#NonNull Context context) {
...
}
public BottomSheetDialog(#NonNull Context context, #StyleRes int theme) {
...
}
So you are missing a context parameter:
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context);
I want to show locations name both in English and local language like Maps app show in android.
Here is my code
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
private final static String TAG = MapsActivity.class.getSimpleName();
private GoogleMap mMap;
private SupportMapFragment mapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng dhaka = new LatLng(23.8103, 90.4125);
mMap.addMarker(new MarkerOptions().position(dhaka));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dhaka,10f));
}
}
But when i run this code all locations name in map is in English. But i want to the output like this
here where the location name is both in English and Bengali.
You can get the necessary info from the following links
https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRegionCodes
http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
I've been trying to setup a map using some help I got here. SO, it's very simple code but crashes. What am I doing wrong?
public class ShowDirection extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap myMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_directions);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); // causes the Exception
}
#Override
public void onMapReady(final GoogleMap map) {
this.myMap = map;
myMap.setMyLocationEnabled(true);
}
Double check if your Fragment's id is actually R.id.map because it's returning null on that line. If it is not, simply replace that with the proper id.
My map is working fine.However, i want to add a satellite view along with my normal view? How can i achieve that?
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng location = new LatLng(x,y);
mMap.addMarker(new MarkerOptions().position(ReduitBusStop).title("you are here!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
Try with setting the type of map tiles as below
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Use the below function to set the satellite view
setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_SATELLITE);
I am getting a java.lang.NullPointerException on the line map.setOnMyLocationChangeListener(myLocationChangeListener);. Strangely, this exception arises while running it some devices while in some it doesn't. How do I solve this?
public class MyMap extends ActionBarActivity implements
RoutingListener, OnMapReadyCallback,
SensorEventListener {
// private GoogleApiClient mGoogleApiClient;
protected GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
}
mapFragment.getMapAsync(this);
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
}
EDIT:
#Override
public void onMapReady(GoogleMap mMap) {
map = mMap;
mUiSettings = map.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mUiSettings.setCompassEnabled(true);
updateMyLocation();
}
By calling mapFragment.getMapAsync(this);, I suppose you have to override this method public void onMapReady(GoogleMap googleMap). Set your mMap = googleMap there and set all other listener there. Keep in mind that your Map object needs some time to be initialised, that's why they give you getMapAsync method.
Editted:
Please remove this from your onCreated
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
and put them map.setOnMyLocationChangeListener(myLocationChangeListener); inside your onMapReady() after this line map = mMap. Please acknowledge when your map is initialized.
When you work with GoogleMap then it takes time to initialize. Sometimes it takes longer time in some devices than others.
To solve this issue, you should initialize the GoogleMap object with some delay. You can follow the below strategy:
private Handler handler = new Handler();
private Runnable mapChecker = new Runnable() {
#Override
public void run() {
try {
if (mapFragment.getMap() != null) {
map = mapFragment.getMap();
map.setOnMyLocationChangeListener(myLocationChangeListener);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
handler.postDelayed(mapChecker, 500);
}
};
Then from onCreate(),
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
..........
handler.postDelayed(mapChecker, 500);
..........
}