Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 服务绑定返回true,但服务对象为null_Android_Service - Fatal编程技术网

Android 服务绑定返回true,但服务对象为null

Android 服务绑定返回true,但服务对象为null,android,service,Android,Service,在什么情况下,对bindService的调用会返回true,但onServiceConnected永远不会运行,从而使我的服务对象为null 代码 // Xmpp Connection Service Binding private BackgroundXmppConnector mService; private boolean mBound = false; private XmppBinder binder; // SERVICE CONNECTION ///////////////

在什么情况下,对
bindService
的调用会返回true,但
onServiceConnected
永远不会运行,从而使我的服务对象为null

代码

// 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

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)

当主UI线程调用
bindService()
时,不能保证调用
ServiceConnection
回调。相对于此,连接回调是异步的,因此在
bindService()
返回true之后,
mService
不会立即有值。
true
值只是指示绑定发生了,但在连接回调被命中之前,您还无法建立完整连接。

我现在已通过使用以下代码解决了此问题:

        // bind to the xmpp service
        Intent iXmpp = new Intent(getApplicationContext(), BackgroundXmppConnector.class);
        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);

我真的不明白这为什么有效,但它确实有效。现在我很高兴:)

Main.java的相关部分已经包括在内了。其他一切都与我的问题无关。