安卓:`java.lang.IllegalArgumentException异常:服务时的onStop()都没有请registered` [英] Android: `java.lang.IllegalArgumentException: Service not registered` when onStop() called

查看:717
本文介绍了安卓:`java.lang.IllegalArgumentException异常:服务时的onStop()都没有请registered`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这势必会长时间运行的服务的应用程序。
我需要确保当用户导航从活动远了服务停止。

I have an applications which is bound to a long running Service. I need to make sure that when the user navigates away from the Activity the Service stops.

所以我实现了的onStop()来关闭该服务:

Therefore I implemented the onStop() to close the service:

这里是code:

@Override    
protected void onStop() {
super.onStop();     
if(mService!=null)mService.stop();
stopService(new Intent(this, LocalService.class));
unbindService(mConnection);
stopService(intent);

}   

下面是我的LogCat中:

Here is my LogCat:

02-22 11:42:44.393: E/AndroidRuntime(1006): FATAL EXCEPTION: main
02-22 11:42:44.393: E/AndroidRuntime(1006): java.lang.RuntimeException: Unable to destroy activity {com.example.quotes/com.example.quotes.Quotes}: java.lang.IllegalArgumentException: Service not registered: com.example.quotes.Quotes$1@40cebd80
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3451)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3469)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.access$1200(ActivityThread.java:141)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.os.Looper.loop(Looper.java:137)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at dalvik.system.NativeStart.main(Native Method)
02-22 11:42:44.393: E/AndroidRuntime(1006): Caused by: java.lang.IllegalArgumentException: Service not registered: com.example.quotes.Quotes$1@40cebd80
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:921)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ContextImpl.unbindService(ContextImpl.java:1451)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.content.ContextWrapper.unbindService(ContextWrapper.java:484)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at com.example.quotes.Quotes.onDestroy(Quotes.java:420)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.Activity.performDestroy(Activity.java:5273)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3438)
02-22 11:42:44.393: E/AndroidRuntime(1006):     ... 11 more

感谢您的帮助!

推荐答案

的一点是,在的onStop()的onDestroy()随后被调用。

The point is that after the onStop(), onDestroy() was called subsequently.

但我的的onDestroy()
是:

@Override    
protected void onDestroy() {
    super.onDestroy(); 
    if(mService!=null)mService.stop();
    stopService(new Intent(this, LocalService.class));
    unbindService(mConnection);
    stopService(intent);        

 }

鉴此,我试图关闭服务的两倍!

THEREFORE I was trying to close the Service twice !

但服务的onStop已经断开()

不管怎样,谢谢!

这篇关于安卓:`java.lang.IllegalArgumentException异常:服务时的onStop()都没有请registered`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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