Android java.lang.IllegalArgumentException:服务未注册 [英] Android java.lang.IllegalArgumentException: Service not registered

查看:284
本文介绍了Android java.lang.IllegalArgumentException:服务未注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的设置:

I have a setup that looks something like this:

class MyFragment implements SomeEventListener {

    Application mAppContext;    

    boolean mBound;
    boolean mDidCallUnbind;
    MyIBinder mBinder;
    ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBound = true;
            mBinder = (MyIBinder) service;
            mBinder.getThings();...
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mDidCallUnbind = false;
            mBound = false;
            mBinder = null;
        }
    };

    ...

    @Override
    public void onSomeEvent() {
        mAppContext.bindService(...);
    }

    void unbindService() {
        if (mBound && !mDidCallUnbind) {
            mDidCallUnbind = true;
            mAppContext.unbindService(mConnection);
        }
    }

    @Override
    public void onPause() {
        unbindService();
        super.onPause();
    }
}

但是,我仍然不时看到标题中的错误:调用unbindService()时会生成java.lang.IllegalArgumentException: Service not registered.我是不是很想念一些东西,还是还有更多的事情发生?我应该指出,可能存在不止一个相同的片段.

However, I am still seeing the error in the title from time to time: java.lang.IllegalArgumentException: Service not registered being generated when unbindService() is called. Am I missing something silly, or is there more going on? I should note that there may be more than one of this same fragment in existence.

编辑

由于似乎没有人真正在阅读代码,所以让我解释一下.除非服务已绑定(mBound)并且 之前未曾调用过unbindService(),否则它不会调用Context.unbindService(ServiceConnection),之前可能未调用onServiceDisconnected(...) .

Since no one actually seems to be reading the code, let me explain. unbindService() does not call Context.unbindService(ServiceConnection) unless the service is bound (mBound) and it had not previously been called before the onServiceDisconnected(...) callback was hit from a possible previous call to unbindService().

请记住,在任何情况下,Android都会为您取消绑定服务,使该服务变为未绑定,但不会调用onServiceDisconnected,从而使我处于陈旧状态吗?

That in mind, are there any cases where Android will unbind your service for you such that the service would become unbound but onServiceDisconnected would not be called thus leaving me in a stale state?

此外,我正在使用我的Application上下文进行初始绑定.假设像这样:

Also, I am using my Application context to do the initial binding. Assume something like:

@Override
public void onCreate() {
    mApplication = getContext().getApplicationContext();
}

推荐答案

我知道这个问题已经得到解答.但是我认为人们有理由弄错这个错误的原因.

I realize this question has already been answered. But I think there is reason to go into why people are making this mistake.

问题确实出在培训文档上. http://developer.android.com/reference/android/app/Service.html 显示正确的实现,而 https://developer.android.com/"ActivityMessenger"中的guide/components/bound-services.html 显示了一个非常不正确的实现.

The issue is really with the training docs. http://developer.android.com/reference/android/app/Service.html shows a correct implementation while https://developer.android.com/guide/components/bound-services.html in the 'ActivityMessenger' shows a Very INCORRECT implementation.

在"ActivityMessenger"示例中,可能会在实际绑定服务之前调用onStop().

In the 'ActivityMessenger' example onStop() could potentially be called before the service has actually been bound.

造成这种混乱的原因是,他们在不同的示例中使用绑定的服务布尔值来表示不同的事物. (主要是,被称为OR的bindService()是实际连接的服务)

The reason for this confusion is they are using the bound service boolean to mean different things in different examples. (mainly, was bindService() called OR is the Service actually connected)

在基于绑定布尔值完成unbind()的正确示例中,绑定布尔值指示 bindService()被调用.由于已排队等待主线程执行,因此无论何时(如果有)onServiceConnected()发生,都需要调用unbindService()(因此要排队执行).

In the correct examples where unbind() is done based on the value of the bound boolean, the bound boolean indicates that the bindService() was called. Since it's queued up for main thread execution, then unbindService() needs to be called (so queued to be executed), regardless of when (if ever) onServiceConnected() happens.

在其他示例中,例如 http://developer.android中的示例.com/reference/android/app/Service.html .边界指示服务已实际绑定,因此您可以使用它,而不会获取NullPointerException.请注意,在此示例中,仍会进行unbindService()调用,并且 bound布尔值不会确定是否取消绑定.

In other examples, such as the one in http://developer.android.com/reference/android/app/Service.html. The bound indicates that the Services is Actually bound so that you can use it and not get a NullPointerException. Note that in this example, the unbindService() call is still made and the bound boolean doesn't determine whether to unbind or not.

这篇关于Android java.lang.IllegalArgumentException:服务未注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆