0
在服務類:爲什麼嘗試使用Android本地綁定服務時出現空指針?
class AeroBluetoothService extends Service { ...
private final IBinder asBleBinder = new LocalBinder();
public class LocalBinder extends Binder {
AeroBluetoothService getService() {
return AeroBluetoothService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return asBleBinder;
}
在客戶活動:
Intent bindAsBleIntent;
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
AeroBluetoothService.LocalBinder asBleBinder = (AeroBluetoothService.LocalBinder) service;
asBleServiceRef = asBleBinder.getService();
}
在客戶端的onCreate():
bindAsBleIntent = new Intent(getApplicationContext(), AeroBluetoothService.class);
bindService(bindAsBleIntent, /*ServiceConnection*/ this, Context.BIND_AUTO_CREATE);
的問題是,當我嘗試調用服務來自客戶端的方法:
asBleServiceRef.scanForAero();
對服務實例的引用,asBleServiceRef
,是null
。這就好像onServiceConnected()
回調沒有被調用(或者傳遞一個空參數)。
我從Android示例中仔細複製了此代碼。我剛剛注意到,該示例從onStart()
方法調用bindService()
,而我從onCreate()
調用。這可能會有所作爲嗎?有什麼問題?
在此處發佈logcat輸出 –
發佈Activity類代碼,您是否在Manifest中聲明該服務? – avinash
賓果!忘了所有關於清單。我會研究和補充。 –