【问题标题】:No suitable method found for requestLocationUpdates没有为 requestLocationUpdates 找到合适的方法
【发布时间】:2015-03-06 03:59:31
【问题描述】:

这是我的 MainActivity.java:

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 {



    @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();
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
}

这是我的 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;

    public void onLocationChanged(Location loc) {
        //editLocation.setText("");
        //pb.setVisibility(View.INVISIBLE);
        Toast.makeText(
                getBaseContext(),
                "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                        + loc.getLongitude(), Toast.LENGTH_SHORT).show();
        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) {}


}

我得到的错误是:

Error:(40, 24) error: no suitable method found for requestLocationUpdates(String,int,int,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)

我对应该修复代码的哪一部分完全空白,我已经尝试谷歌但找不到相关的解决方案。

【问题讨论】:

标签: java android


【解决方案1】:

您使用了错误的LocationListener 类。你需要替换这个:

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

用这个:

import android.location.LocationListener;

com.google.android.gms.location 的内容是用于 FusedLocationProviderApi,它是 Google Play 服务的一部分。

标准的 Android 位置信息位于 android.location

另外,你有GetCurrentLocation extends Activity。但它并不是真正的Activity。您需要删除 extends Activity 子句。这将导致问题,因为您在该类的某些方法中调用getBaseContext()。要解决此问题,请让GetCurrentLocation 的构造函数采用Context 参数并将其保存在私有成员变量中。然后在您需要 Context 而不是调用 getBaseContext() 时使用它。

MainActivity中,当你创建一个新的GetCurrentLocation时,你可以这样做:

LocationListener locationListener = new GetCurrentLocation(this);

this 作为Context 参数传递(Activity 扩展Context)。

【讨论】:

    【解决方案2】:

    错误告诉你没有接受参数(String,int,int,com.google.android.gms.location.LocationListener)的重载方法requestLocationUpdates

    更接近的方法是

    requestLocationUpdates(String provider, long minTime, float minDistance,
                           LocationListener listener);
    

    因此,解决您的错误的可能解决方案是:

    long t = 5000;
    float d = 10;
    locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, t, d, locationListener);
    

    更多详情请参考Android LocationManager

    【讨论】:

    • 好的,我已经这样做了,我得到了一个新错误,即 Error:(23, 8) 错误:MainActivity 不是抽象的,并且不会覆盖 LocationListener 中的抽象方法 onLocationChanged(Location)
    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2014-08-27
    • 2021-06-19
    • 2015-11-26
    • 2019-09-26
    相关资源
    最近更新 更多