0
我正試圖調用服務,並根據響應將用戶重定向到不同的活動(登錄名)。在onResume中調用服務時出現NullPointerException
如果我等待做,直到說,點擊一個按鈕,然後它工作正常(因爲服務綁定),但如果我這樣做的onResume,然後我得到以下異常:
ERROR/AndroidRuntime(2226): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {...MyActivity}:
java.lang.NullPointerException
我的代碼是:
FooService fooService;
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, FooService.class);
bindService(intent, fooServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onResume() {
//I would assume the service would have bound by now?
super.onResume();
doSomething();
}
@UBM:如果您來自Javascript背景,PhoneGap可能會是您的更好選擇。 「它正在做網絡I/O ......你能提供一些可以幫助我做到的資源嗎?」 - 這是一個'IntentService',它接收下載文件的命令,然後將其下載並在下載完成後讓活動知道結果:https://github.com/commonsguy/cw-android/tree/主/服務/下載器「,如果我依賴這項服務,在準備好後我怎麼能打電話給它?」 - 從'onServiceConnected()'調用它。 – CommonsWare 2011-04-24 15:03:18