EDIT: I have initialised the BreakDownOnMaps object now
I'm getting the following error:
E/AndroidRuntime: 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
E/AndroidRuntime: at com.example.yomac_000.rsrpechhulp.BreakDownOnMaps.handleNewLocation(BreakDownOnMaps.java:57)
E/AndroidRuntime: at utils.MyLocationListener.onConnected(MyLocationListener.java:52)
And it's because of this line of code:
gMap.addMarker(options);
And this of line of code:
breakDownOnMaps.handleNewLocation(location);
Below you can see my code:
MyLocationListener.java:
public class MyLocationListener implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private final Context context;
private GoogleMap mMap;
private BreakDownOnMaps breakDownOnMaps = new BreakDownOnMaps();
protected GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest = new LocationRequest();
public MyLocationListener(Context context) {
this.context = context;
buildApi();
}
@Override
public void onLocationChanged(Location location) {
breakDownOnMaps.handleNewLocation(location);
}
@Override
public void onConnected(Bundle bundle) {
System.out.println("onConnected");
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
}
else {
breakDownOnMaps.handleNewLocation(location);
};
}
private void buildApi() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public void connect() {
mGoogleApiClient.connect();
}
}
And here is the code of BreakDownOnMaps.java:
public class BreakDownOnMaps extends FragmentActivity implements
OnMapReadyCallback {
double currentLatitude;
double currentLongitude;
LatLng latLng;
GoogleMap gMap;
private MyLocationListener myLocationListener;
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_break_down_on_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
myLocationListener = new MyLocationListener(this);
}
@Override
protected void onStart() {
super.onStart();
myLocationListener.connect();
}
@Override
protected void onResume() {
super.onResume();
myLocationListener.connect();
}
public void handleNewLocation(Location loc) {
currentLatitude = loc.getLatitude();
currentLongitude = loc.getLongitude();
latLng = new LatLng(currentLatitude, currentLongitude);
System.out.println("handleNewLocation ");
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
gMap.addMarker(options);
gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
@Override
public void onMapReady(GoogleMap googleMap) {
System.out.println("currentLatitude : " + currentLatitude);
System.out.println("currentLongitude : " + currentLongitude);
latLng = new LatLng(currentLatitude, currentLongitude);
setgMap(googleMap);
if(currentLatitude != 0 || currentLongitude != 0) {
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
googleMap.addMarker(options);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
public void setgMap(GoogleMap gMap) {
this.gMap = gMap;
}
}
Before refactoring I had put all of my code in BreakDownOnMaps.java. And everything did worked fine. So if you want to see how it looks before you can see it through the link below:
Link: http://pastebin.com/p6c3UeSC
I had also heared that the art of refactoring is not adding code, but only to change the code in the way it looks like. So I really don't know what I'm doing wrong.