Sir,谢谢你的帮助,这是我的程序的完整代码。嗨,我收到错误"无法解析方法locationmanager.requestLocationUpdates();
public class MainActivity extends Activity implements LocationListener {
public Button button;
private TextView textView;
public LocationManager locationmanager;
Context context;
private double lat,lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationmanager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//error !!!!!!
**错误**locationmanager.requestLocationUpdates(locationmanager.GPS_PROVIDER,5000,0,this);
}
});
}
@Override
public void onLocationChanged(Location location) {
if(location!=null)
{
lat=location.getLatitude();
lon=location.getLongitude();
textView.append("n lat:"+lat+" lon:"+lon);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
首先,不要使用"gps"
。使用LocationManager.GPS_PROVIDER
。
其次,this
与requestLocationUpdates()
的任何签名都不匹配。this
是View.OnClickListener
,而不是LocationListener
或PendingIntent
。
如果此代码来自某个活动或片段,并且该活动或片段正在实现LocationListener
,则使用此MyActivity.this
而不是this
,其中MyActivity
是正在实现LocationListener
的活动或片段的名称。