0

This is my MainActivity

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.location.LocationListener;
    import com.google.android.gms.location.LocationRequest;



    public class MainActivity extends Activity implements LocationListener {



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener locationListener = new GetCurrentLocation();
            long t = 5000;
            float d = 10;
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, t, d, locationListener);
    }
}

This is my GetCurrentLocation.java

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GetCurrentLocation extends Activity implements LocationListener {
    String longitude;
    String latitude;
    String cityName;


    @Override
    public void onLocationChanged(Location loc) {
        //editLocation.setText("");
        //pb.setVisibility(View.INVISIBLE);
        longitude = "Longitude: " + loc.getLongitude();
        //Log.v(TAG, longitude);
        latitude = "Latitude: " + loc.getLatitude();
        //Log.v(TAG, latitude);

        /*------- To get city name from coordinates -------- */
        //String cityName = null;
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses;
        try {
            addresses = gcd.getFromLocation(loc.getLatitude(),
                    loc.getLongitude(), 1);
            if (addresses.size() > 0)
                System.out.println(addresses.get(0).getLocality());
            cityName = addresses.get(0).getLocality();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        //editLocation.setText(s);
    }

    //@Override
    public void onProviderDisabled(String provider) {}

    //@Override
    public void onProviderEnabled(String provider) {}

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


}

These are the errors.

Error: MainActivity is not abstract and does not override abstract method onLocationChanged(Location) in LocationListener
Error:(36, 24) error: no suitable method found for requestLocationUpdates(String,long,float,com.google.android.gms.location.LocationListener)
method LocationManager.requestLocationUpdates(long,float,Criteria,PendingIntent) is not applicable
(actual argument String cannot be converted to long by method invocation conversion)
method LocationManager.requestLocationUpdates(String,long,float,PendingIntent) is not applicable
(actual argument com.google.android.gms.location.LocationListener cannot be converted to PendingIntent by method invocation conversion)
method LocationManager.requestLocationUpdates(long,float,Criteria,android.location.LocationListener,Looper) is not applicable
(actual and formal argument lists differ in length)
method LocationManager.requestLocationUpdates(String,long,float,android.location.LocationListener,Looper) is not applicable
(actual and formal argument lists differ in length)
method LocationManager.requestLocationUpdates(String,long,float,android.location.LocationListener) is not applicable
(actual argument com.google.android.gms.location.LocationListener cannot be converted to android.location.LocationListener by method invocation conversion)

Basically the 2 main errors are MainActivity is not abstract and also the request location updates. Please help. All I want is to get the current location's Latitude and Longitude of the user.

SOFT1234
  • 181
  • 1
  • 6
  • why don't you implements those methods – Blackbelt Mar 06 '15 at 14:44
  • Which bit of the error message do you not understand? You've said that `MainActivity` implements `LocationListener`, but you haven't actually done so. Then the errors around `requestLocationUpdates` seem to be that you're providing inappropriate arguments - check the documentation. – Jon Skeet Mar 06 '15 at 14:44
  • @JonSkeet how do I fix it ? can you please provide some samples ? – SOFT1234 Mar 06 '15 at 14:48
  • Why the `MainActivity` implements `LocationListener` when you are not implementing `onLocationChanged` in it? The first line in the error tells you just that... – miselking Mar 06 '15 at 14:50
  • 2
    Either stop saying that `MainActivity` implements `LocationListener`, or actually implement the methods. Do you understand what it means for a class to say that it implements an interface? If you're very new to Java, I'd actually recommend writing straight console apps in Java before doing anything in mobile or web - that way you won't get out of your depth as easily when you *do* move on to more advanced APIs. – Jon Skeet Mar 06 '15 at 14:53

0 Answers0