Java 找不到适用于RequestLocationUpdate的方法
这是我的MainActivity.java:Java 找不到适用于RequestLocationUpdate的方法,java,android,Java,Android,这是我的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.Loca
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) {}
}
我完全不知道我应该修复代码的哪一部分,我尝试过谷歌搜索,但没有找到相关的解决方案。错误告诉您没有重载方法
requestLocationUpdates
接受参数(String,int,int,com.google.android.gms.location.LocationListener)
更接近的方法是
requestLocationUpdates(String provider, long minTime, float minDistance,
LocationListener listener);
因此,修复错误的可能解决方案是:
long t = 5000;
float d = 10;
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, t, d, locationListener);
有关更多详细信息,请参阅您使用的
位置侦听器类错误。您需要替换此:
import com.google.android.gms.location.LocationListener;
为此:
import android.location.LocationListener;
com.google.android.gms.location
内容用于FusedLocationProviderApi
,它是google play服务的一部分
标准的Android位置信息在Android.location
中
此外,还有GetCurrentLocation扩展活动
。但这并不是真正的活动
。您需要删除扩展活动
子句。这将导致问题,因为您正在该类的某些方法中调用getBaseContext()
。要解决此问题,请让GetCurrentLocation
的构造函数获取Context
参数,并将其保存在私有成员变量中。然后在需要上下文时使用它,而不是调用getBaseContext()
在MainActivity
中,创建新的GetCurrentLocation
时,可以执行以下操作:
LocationListener locationListener = new GetCurrentLocation(this);
将this
作为Context
参数传递(Activity
扩展Context
)。好的,我已经这样做了,我得到了一个新的错误,即错误:(23,8)错误:MainActivity不是抽象的,并且不会覆盖locationListener中locationChanged(Location)的抽象方法使用此链接,我个人觉得这个很有用使用这个链接,我个人觉得这个很有用
LocationListener locationListener = new GetCurrentLocation(this);