1

i'm new into android, please help. I'm trying to run onLocationChanged(Location location) inside a Service. so that it will make marker all the time until stop button is clicked.

public class MyService extends Service implements LocationListener {
    // variable peta
    GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    private GoogleMap mMap;
    Location location;

    // parameter untuk di masukin ke database
    TextView tvlattitude;
    TextView tvlongitude;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        onLocationChanged(location);
        // Let it continue running until it is stopped.
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onLocationChanged(Location location) {
        double lattitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        mMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                .position(latLng)
                .title("Kamu disini"));

        tvlattitude.setText(""+lattitude);
        tvlongitude.setText(""+longitude);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

this is i have tried, but its said, Null Pointer Exception on location what cause it? need help.

stuck
  • 57
  • 8
  • Well if `location==null` then you will get a NullPointerException apparently. On which statement is that? – greenapps May 03 '18 at 09:08
  • `onLocationChanged(location);`. My god... You are calling onLocationChanged() yourself and even with a parameter that is not initialized and hence has a null value. You are not supposed to call onLocationChanged() yourself. Its a listener. It will be called by os if the location changes. – greenapps May 03 '18 at 09:12
  • And if you wait for onLocationChanged() to be triggered you will still have null pointers for mMap, tvlatitude and tvlongitude. All are uninitialised. – greenapps May 03 '18 at 09:15
  • the reason i'm using service is when i go to the previous activity, and then click the map again, the markers all gone. i have to start all over again. but actually it is not gone because automatically called by system as you said,right? so, what are you suggesting, just use onPause?and onResume? on mapsActivity? what should i write with the onLocationChanged with onPause(){ keep my map activity alive.} and onResume(){resume mark mapping after onPause}. – stuck May 03 '18 at 10:12
  • or should i just use a service like above, and delete onLocationChanged(location) onStartCommand, and initialized mMap, tvlatitude and tvlongitude ? – stuck May 03 '18 at 10:15
  • `and delete onLocationChanged(location) ` Yes. you should delete that line. – greenapps May 03 '18 at 10:46
  • `and initialized mMap, tvlatitude and tvlongitude ?` Yes. But sadly there is no way you could initialize them. Just try though and you will see. – greenapps May 03 '18 at 10:47
  • appreciate your help man, i'll just go trial and error, let's see what happen. basically all in mapsActivity now in MyService.class, because it all connected in mMap, tvlatitude and tvlongitude – stuck May 03 '18 at 10:54
  • To help you a little bit more you should read this post "Communication between Service and Activity" https://stackoverflow.com/questions/20594936/communication-between-activity-and-service. In short: to send messages from activity to service you call startService again with message as putExtra(). To send messages from service to activity the service sends a broadcast for which the activity has registered a broadcast receiver. – greenapps May 03 '18 at 10:59
  • So you better remove all those textviews and so from the service. – greenapps May 03 '18 at 11:00

0 Answers0