我创建了一个使用LocationListener的类,但是当我请求位置更新时,我得到了classCastException
这是我的班级代码
public class Loc implements
LocationListener, GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
// A request to connect to Location Services
private LocationRequest mLocationRequest;
// Stores the current instantiation of the location client in this object
private LocationClient mLocationClient;
/*
* Note if updates have been turned on. Starts out as "false"; is set to
* "true" in the method handleRequestSuccess of LocationUpdateReceiver.
*/
boolean mUpdatesRequested = false;
// Milliseconds per second
public static final int MILLISECONDS_PER_SECOND = 1000;
// The update interval
public static final int UPDATE_INTERVAL_IN_SECONDS = 10;
// A fast interval ceiling
public static final int FAST_CEILING_IN_SECONDS = 1;
// Update interval in milliseconds
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = MILLISECONDS_PER_SECOND
* UPDATE_INTERVAL_IN_SECONDS;
// A fast ceiling of update intervals, used when the app is visible
public static final long FAST_INTERVAL_CEILING_IN_MILLISECONDS = MILLISECONDS_PER_SECOND
* FAST_CEILING_IN_SECONDS;
Location currentLocation ;
LocationListener locListener;
Context x;
public Loc(Context con) {
// Create a new global location parameters object
this.x=con;
mLocationRequest = LocationRequest.create();
/*
* Set the update interval
*/
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest
.setFastestInterval(FAST_INTERVAL_CEILING_IN_MILLISECONDS);
// Note that location updates are off until the user turns them on
mUpdatesRequested = false;
/*
* Create a new location client, using the enclosing class to handle
* callbacks.
*/
mLocationClient = new LocationClient(con, this, this);
}
public void stop() { // If the client is connected
if (mLocationClient.isConnected()) {
stopPeriodicUpdates();
}
// After disconnect() is called, the client is considered "dead".
mLocationClient.disconnect();
}
public void start() {
mLocationClient.connect();
}
public void startUpdates() {
mUpdatesRequested = true;
startPeriodicUpdates();
}
public Location getLocation() {
// Get the current location
currentLocation = mLocationClient.getLastLocation();
// Display the current location in the UI
Log.e("lat log", "" + currentLocation.getLatitude() + " , "
+ currentLocation.getLongitude());
return currentLocation;
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
currentLocation=location;
Log.e("lat log", "" + currentLocation.getLatitude() + " , "
+ currentLocation.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
private void stopPeriodicUpdates() {
mLocationClient
.removeLocationUpdates((com.google.android.gms.location.LocationListener) this);
}
private void startPeriodicUpdates() {
mLocationClient.requestLocationUpdates(mLocationRequest,(com.google.android.gms.location.LocationListener) this);
}
}
这是我的活动代码
public class MainActivity1 extends Activity {
Loc l;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
t=(TextView)findViewById(R.id.txt);
l = new Loc(getApplicationContext());
l.start();
}
public void getLoc(View v) {
// l.getLocation();
l.startUpdates();
}
我的拉特和长期完全没问题。 但我的问题是位置更新
我收到此错误
Caused by: java.lang.ClassCastException: com.example.gps.Loc cannot be cast to com.google.android.gms.location.LocationListener
我知道我不能像这样传递LocationListener
mLocationClient.requestLocationUpdates(mLocationRequest,(com.google.android.gms.location.LocationListener) this);
但我应该怎样完全传递它?
答案 0 :(得分:4)
只需要添加界面
com.google.android.gms.location.LocationListener
答案 1 :(得分:0)
public class Loc implements
GooglePlayServicesClient.ConnectionCallbacks
,GooglePlayServicesClient.OnConnectionFailedListener
,com.google.android.gms.location.LocationListener {
//...
}