嗨,我收到错误"无法解决方法locationmanager.requestLocationUpdates();

时间:2015-11-14 19:32:51

标签: android locationmanager

爵士 感谢您的帮助,这是我的程序的完整代码。 嗨,我收到错误"无法解决方法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) {

}

}

1 个答案:

答案 0 :(得分:1)

首先,不要使用"gps"。使用LocationManager.GPS_PROVIDER

其次,thisrequestLocationUpdates()的任何签名都不匹配。 thisView.OnClickListener,而不是LocationListenerPendingIntent

如果此代码来自活动或片段,并且该活动或片段正在实施LocationListener,则此MyActivity.this代替this,其中MyActivity是其名称正在实施LocationListener的活动或片段。