Obtengo una excepción de puntero nulo en la línea mService.start() cuando intento enlazar a un servicio ya iniciado. Hago lo mismo desde diferentes actividades (donde se inicia el servicio) todo va bien. Todas estas actividades son parte de una aplicación.android bindservice
¿Qué crees que hago mal?
public class RouteOnMap extends MapActivity{
private static final int NEW_LOCATION = 1;
private static final int GPS_OFF = 2;
private MapView mMapView;
private ILocService mService;
private boolean mServiceStarted;
private boolean mBound;
private Intent mServiceIntent;
private double mLatitude, mLongitude;
private ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder iservice) {
mService = ILocService.Stub.asInterface(iservice);
mBound = true;
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
mBound = false;
}
};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setBuiltInZoomControls(true);
mServiceIntent = new Intent();
mLatitude = 0.0;
mLongitude = 0.0;
mBound = false;
}
@Override
public void onStart(){
super.onStart();
mServiceIntent.setClass(this, LocationService.class);
//startService(mServiceIntent);
if(!mBound){
mBound = true;
this.bindService(mServiceIntent, connection, Context.BIND_AUTO_CREATE);
}
}
@Override
public void onResume(){
super.onResume();
try {
mService.start();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onPause(){
super.onPause();
if(mBound){
this.unbindService(connection);
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Gracias. Llamar a mService.start() desde onServiceConnected funcionó. ¿Recomienda llamar métodos mService siempre desde onServiceConnected()? – mnish
Hasta que se llame a 'onServiceConnected()', 'mService' es' null'. Por lo tanto, no desea llamar a métodos en 'mService' hasta que esté seguro de que se habrá llamado a' onServiceConnected() '. Entonces, por ejemplo, para cuando el usuario pueda hacer clic en algo, 'mService' probablemente esté listo. Sin embargo, 'onResume()' es demasiado pronto, es posible que su solicitud 'bindService()' aún no se haya procesado. – CommonsWare
muchas gracias por su ayuda – mnish