在什麼情況下調用bindService
返回true,但onServiceConnected
從不運行,從而使我的服務對象爲空?服務綁定返回true,但服務對象爲空
代碼
// Xmpp Connection Service Binding
private BackgroundXmppConnector mService;
private boolean mBound = false;
private XmppBinder binder;
// SERVICE CONNECTION //////////////////////////////////////////////////////////////////////////
private ServiceConnection mConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
Log.i("Main", "Service is connected");
binder = (XmppBinder)service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName className)
{
mBound = false;
}
};
和我如何與服務
// bind to the xmpp service
Intent iXmpp = new Intent(getApplicationContext(), BackgroundXmppConnector.class);
if(bindService(iXmpp, mConnection, Context.BIND_AUTO_CREATE))
{
Log.i("Main", "Status of bind: " + mBound + " and service connection: " + mService.toString());
// Request from Xmpp
iXmpp.putExtra("MESSAGEDATA", new Gson().toJson(
Utility.makeTransaction(getApplicationContext(), MessageType.Type.POPULATE, pop)
));
mService.sendMessage(iXmpp);
// unbind from our service
unbindService(mConnection);
}
和NPE發生的歷史交流的時候我檢查service
對象的狀態,綁定後
03-25 12:36:33.349: E/AndroidRuntime(19638): FATAL EXCEPTION: main
03-25 12:36:33.349: E/AndroidRuntime(19638): java.lang.NullPointerException
03-25 12:36:33.349: E/AndroidRuntime(19638): at com.goosesys.gaggle.Main.onKeyMultiple(Main.java:439)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.view.KeyEvent.dispatch(KeyEvent.java:2644)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.app.Activity.dispatchKeyEvent(Activity.java:2361)
03-25 12:36:33.349: E/AndroidRuntime(19638): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1887)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3577)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3533)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3115)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4157)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4136)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2932)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.os.Looper.loop(Looper.java:137)
03-25 12:36:33.349: E/AndroidRuntime(19638): at android.app.ActivityThread.main(ActivityThread.java:4810)
03-25 12:36:33.349: E/AndroidRuntime(19638): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:36:33.349: E/AndroidRuntime(19638): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 12:36:33.349: E/AndroidRuntime(19638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-25 12:36:33.349: E/AndroidRuntime(19638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-25 12:36:33.349: E/AndroidRuntime(19638): at dalvik.system.NativeStart.main(Native Method)
Main.java的相關部分已包含在內。其他一切都與我的問題完全無關。 – LokiSinclair