6
Zaten başlatılmış bir hizmete bağlanmaya çalıştığımda mService.start() satırında null pointer istisnası alıyorum. Aynı şeyi farklı faaliyetlerden (hizmetin başladığı yer) ben yapıyorum. Bütün bu aktiviteler bir uygulamanın parçasıdır.android bindservice
Sence yanlış mıyım?
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;
}
}
Teşekkür ederiz. OnServiceConnected çağrısından mService.start() çağrıldı. MService yöntemlerini her zaman onServiceConnected() yönteminden çağırmak ister misiniz? – mnish
onServiceConnected() 'kadar,' mService' null' olarak adlandırılır. Bu nedenle, 'mService' yöntemini çağırmak istemediğinizden emin olun, 'onServiceConnected() 'çağrılmayacaktır. Yani, örneğin, kullanıcının bir şey üzerine tıklayabileceği zaman, 'mService' muhtemelen hazırdır. Ancak, onResume() 'çok yakında - 'bindService()' isteğiniz henüz işlenmemiş olabilir. – CommonsWare
Yardımınız için çok teşekkürler – mnish