Ok, soy nuevo en el desarrollo de Android y estoy intentando vincularme a un servicio para que pueda llamar a los métodos en el servicio una vez que se haya iniciado. La actividad y el servicio descritos a continuación son parte de la misma aplicación, por lo que no debería haber ningún problema, pero cada vez que ejecuto mi aplicación aparece el siguiente error:Excepción de Android ClassCast al vincular al servicio
java.lang.ClassCastException: android.os.BinderProxy
La línea de esto sucede en es:
LocalBinder binder = (LocalBinder) service;
Mi código de actividad (simplificado es):
public class Main extends Activity {
boolean gpsBound = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** Called whenever the activity is started. */
@Override
protected void onStart() {
super.onStart();
// Bind to GPSService
Intent i = new Intent(this, GPSService.class);
startService(i);
bindService(i, connection, Context.BIND_AUTO_CREATE);
}
/** service binding */
private ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// After binding to GPSService get the instance of it returned by IBinder
LocalBinder binder = (LocalBinder) service;
gpsBound = true;
}
public void onServiceDisconnected(ComponentName className) {
gpsBound = false;
}
};
}
Servicio:
public class GPSService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent i) {
// TODO Auto-generated method stub
return new LocalBinder<GPSService>(this);
}
/**
* Our implementation of LocationListener that handles updates given to us
* by the LocationManager.
*/
public class CustomLocationListener implements LocationListener {
DBHelper db;
CustomLocationListener() {
super();
}
// Overridden methods here...
}
}
Y por último, mi LocalBinder:
/**
* A generic implementation of Binder to be used for local services
* @author Geoff Bruckner 12th December 2009
*
* @param <S> The type of the service being bound
*/
public class LocalBinder<S> extends Binder {
private String TAG = "LocalGPSBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
entiendo el significado de la excepción ClassCast pero no puede entender qué hacer! Seguí el ejemplo en la documentación de google, pero todavía no funciona. ¿Alguien puede arrojar alguna luz sobre lo que podría estar causando esto?
¡Gracias de antemano!
Qué quiere decir gustaría: 'servicio LocalBinder aglomerante = (LocalBinder );' ¡Si es así, todavía da el mismo error! –
Me estaba inclinando a sacar los genéricos por completo. Editaré en una muestra rápida de uso de un servicio enlazado que estoy escribiendo en este momento. –
¡Tendré una oportunidad sin genéricos, pero si pudieras dar un ejemplo sería genial!Gracias, James –